I have a question :

I have Users that have a boolean attribute "Admin".
What I did is basically : "if admin attribute is set on false, then a
writing appears telling that 'you must be an admin to see this zone'".

I wanted to organize things differently, for example:
"if the logged user is logged, and if he has an Admin-attribute set on
true, then he may get to the requested aministration page, else, he'll
be redirected at login page".

I have a current_user method :

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end

but I tried to add it into the controller, the model and application
controller, with just getting error.

Is it possibile to implement, in your opinion?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Radhames brito at Nov 17, 2011 at 2:44 am
    if you are building athentication from scratch this method can be accessed
    by the controller and views by putting that method in the
    application_controller and adding

    def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end
    helper_method :current_user

    this will make it available to other controllers and views, session data is
    not accesible from the models.

    What you want to do is done with a before filter in the controller like
    this:

    at the top of the controllers you add

    before_filter :check_if_cool_enough

    at the bottom

    private
    def check_if_cool_enough
    if current_user.admin?
    flash[:notice] = "YOU ARE SOOO COOL"
    else
    flash[:error] = "omg, lol noob"
    redirect_to root_path
    end
    end

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedNov 17, '11 at 1:06a
activeNov 17, '11 at 2:44a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Leo M.: 1 post Radhames brito: 1 post

People

Translate

site design / logo © 2022 Grokbase