what the best rails way to do the following

I've rails 3 app which displays records from a DB into table. I want the same method to check if the admin controller is used and if so add edit delete options to the table items.

Example: logged in a none admin server/home/list_info

  • Some info name address
  • More info name address

Logged in as Admin sever/admin/list_info

  • Some info name address Edit Delete
  • More info name address Edit Delete

I've currently got two controllers and two view methods, the admin method is just a copy with the Edit Delete links on the end. This doesn't seem very DRY to me. What to people do in this situation ?

Many Thanks Andy

Answers


I think the best way is to have one set of controllers/views and simply see if the current user is an admin. If yes, show the edit/delete links.

Typically, you could use Devise for authentication and CanCan for authorization.

Devise provides a current_user object, so if you implement an admin? method you could use something like

<%= link_to_if current_user.admin?, 'Delete', ... %>

Note: the above uses only Devise, not CanCan.


Need Your Help

Ec2 Passing User Data to Windows Instance at Launch

windows powershell amazon-ec2

There appear to be a couple of different ways to pass data to an Windows server instance at launch. It appears amazon has support for now passing commands to the instance at launch as outlined in t...

Rails aggregating polymorphic has_many relationship

ruby-on-rails polymorphic-associations

In my app, an Account can be owned by a Household or a User. Users can access accounts that they own, and accounts that their household owns. I've defined accessible_accounts, that gives me an ar...