How can I check user admin on routes.rb in Rails? -


i have 2 different views , 2 different controllers in rails app want user redirect specific 1 depending on whether user admin or not.

i saw post this one directs page depending on whether user authenticated or not, looking (i using devise):

#routes.rb   authenticated :user     if user.admin       root to: "admin/first_page#index", as: :authenticated_root     else       root "first_page#index", as: :authenticated_root     end   end    root to: "user/sign_in" 

when user signs in, checks user's admin privilege. if user admin, go admin/first_page#index. if user isn't admin, go first_page#index.

i thought of using 1 page , hide features non-admin, like: <% if user.admin%><%= secret_admin_feature %><% end %> keep dry, have own reasons why choose not keep dry in case.

is possible admin check routes.rb? if yes, how can done? if not, alternative?

i don't think idea check admin privileges in routes. proper way of doing follows:

  • you have application_helper method called is_admin? checks if user administrator or not.

  • you submit sign in form method. need use helper , redirect.

def sign_in   # submit form , stuff here   # , if form submission successful this:       redirect_to admin_first_page_index_path , return if is_admin?   redirect_to first_page_index_path , return end 

that how it.

you still need provide redirect sign in page if form submission failed. , in admin/first_page#index method might want check if user admin , redirect page, force non admin users flow.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -