Tuesday, 15 February 2011

ruby on rails - Should I have a different admin? method for the view and for the before_filter? -



ruby on rails - Should I have a different admin? method for the view and for the before_filter? -

i'm learning rails , working on dinnerdash.

in application_controller.rb have:

helper_method :admin? def admin? current_user.admin_code == 'secret' if current_user end

so utilize if admin? in view files display things admins. want write before_filter checks if current_user admin , if not, redirects.

it seems me have write method this. view files, want method homecoming false if user isn't admin, , before_filter, want redirect.

still, tells me isn't efficient way this. since i'm learning rails, don't want develop bad habits of writing code isn't dry. ideas on how best handle situation?

i create admin? instance method of user model. think belongs there because you're asking info user object.

then, before_filter, this:

before_filter :admin_or_redirect def admin_or_redirect redirect_to some_url if !current_user.admin? end

then can still phone call admin? in views on @user (which assign current_user in controller), , have different behavior before_filter.

edit:

you want alter admin? method this:

class user < activerecord::base ... def admin? self.admin_code == 'secret' end end

ruby-on-rails dry

No comments:

Post a Comment