what is a good way to customize a view based on login credentials. for instance, i''m putting together an app that shows some dynamic tablular data. i''d like ''normal'' users to be able to only *view* the data, but have additional links/buttons for someone logged in as an ''admin'' to *change* the data. creating two separate .rhtml files for each login type seems counter-ruby, so I''m wondering if there''s a more elegant way to do it. thanks in advance les
On 4/15/05, lester <dumpingrounds-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> what is a good way to customize a view based on login credentials. for instance, > i''m putting together an app that shows some dynamic tablular data. i''d like > ''normal'' users to be able to only *view* the data, but have additional links/buttons > for someone logged in as an ''admin'' to *change* the data. creating two separate > .rhtml files for each login type seems counter-ruby, so I''m wondering if > there''s a more elegant way to do it.Simplest way: <% if admin_logged_in? %> display stuff <% end %> Then create a helper function called admin_logged_in? that checks to see if the logged in user is an admin or not. Or, create a partial called _admin_links.rhtml and do the check in there. Lots of different ways to do it.
On 4/15/05, lester <dumpingrounds-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> what is a good way to customize a view based on login credentials. for instance, > i''m putting together an app that shows some dynamic tablular data. i''d like > ''normal'' users to be able to only *view* the data, but have additional links/buttons > for someone logged in as an ''admin'' to *change* the data. creating two separate > .rhtml files for each login type seems counter-ruby, so I''m wondering if > there''s a more elegant way to do it.First, in your login controller, you change the authorize function to return true if the user is admin, or false if the user is not. Then in your view, put in conditionals to test for authorize, if it''s true, put in the edit buttons, if it''s false, don''t. -- Urban Artography http://artography.ath.cx
On Apr 15, 2005, at 2:01 PM, lester wrote:> what is a good way to customize a view based on login credentials. > for instance, i''m putting together an app that shows some dynamic > tablular data. i''d like ''normal'' users to be able to only *view* the > data, but have additional links/buttons for someone logged in as an > ''admin'' to *change* the data. creating two separate .rhtml files for > each login type seems counter-ruby, so I''m wondering if there''s a more > elegant way to do it.I use the following in application_helper.rb: def user_logged_in? @session[''user''] end def is_admin? @session[''user''][''login''] == CONFIG[''admin_login''] end def navigation_bar if user_logged_in? unless is_admin? render_partial "partials/navbar_user" else render_partial "partials/navbar_admin" end else render_partial "partials/navbar_public" end end and the following in my layouts/application.rhtml: <%= navigation_bar %> There are lots of different ways to approach this. You can also use a layout method... e.g.: instead of: layout ''application'', :except => ... list of exceptions you could use: layout :my_layout_method, :except => ... list of exceptions private def my_layout_method if user_logged_in? unless is_admin? "user_layout" else "admin_layout" end else "public_layout" end end .. I hope there are no errors in all these snippets but you get the idea. I''m paraphrasing from what I use. -- Michael Schubert Maintaining badly written code is like trying to solve a crossword puzzle set by someone who can''t spell -- Jacob Kaplan-Moss