Can you share the content of session_help.rb? In lieu of that...
I am offering very simple suggestions, which I think are true for both
Rails 3 and 4. You might want to explore the gem Devise for more spiffy
user authentication.
On creation of a new user, in your database users table, do you set a flag
to indicate whether or not the user is admin, let's say the field is
'is_admin' and 1 indicates admin and 0 indicates not admin.
The Controller informs your Model.
When the user logs in, you have a controller method that manages login.
You try to find the user by params like username, password, email or some
combination...
User Controller:
def login ## or whatever you use...
@user = User.find ( params[:username] .....
unless @user.blank?
session[:user_id] = @user.id ## Something I do...
current_user = @user
## current_user is a helper method, in my case it is located in my
Application Controller... it is an instance of your current session user .
If current_user does not ring familiar, do a search for it on
## your application. It may be in your Application controller. If
you can't find it, let me know... Perhaps, your session_helper.rb is
serving this function/purpose?? Maybe the instance names are not the same.
## Now current_user has been informed of admin status
.....
end
Now in your User model you need a def that returns admin status
Model
def is_admin?
is_admin == 1 ? true : false ## or however you would like
end
So now in views and controllers, you can make a call to
current_user.is_admin?: eg, if current_user.is_admin? ..... content ...
end
Hope this helps.
Liz
-By the way, I have this funny feeling that you may be able to consolidate
your user_temp_table and admin_table tables. Perhaps in utilization of
yes/no flags? Would you might sharing the structure of each?
-And I deleted the above response, my kid startled me and I hit some wrong
keys that caused a posting of an incomplete response.....
On Monday, July 6, 2015 at 9:18:43 AM UTC-4, Padmahas Bn wrote:
>
> Based on railstutorials.org, I've written my method to check whether it
> is admin or not inside sessions_helper.rb. Now in model I've to insert data
> to temporary table lets assume "*user_temp_table*", if it is normal user
> that has logged in. Later those entries will be approved by admin. If it is
> admin himself logged in I want to insert data to permanent table lets
> assume "*admin_table".*
>
> Now where is the best place to call admin checking method which I have
> written inside sessions_helper.rb.
> I have two possibilities.
>
> 1. Set a flag to 1 or 0 inside controller and access that flag variable
> from model if it is admin or user respectively.
> Problem: But I found some blogs and stackoverflow posts stating that
> controller is just a bridge between view and model. And variables inside
> controller should not be accessed from model. Addition to that I don't know
> how to access controller variable from model. If this is the method please
> tell me how?
>
> 2. include SessionHelper inside model and call the method.
>
> But posts I found related to both of these solutions are very old. So in
> rails 4 which is the valid and good way to carry out this task? Or is there
> is any other new way?
>
> Thank you.
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/83439a6b-3a7e-44e6-84f2-92e073083335%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.