Question

I want to create a new page in my rails app that you can access once you're logged in. All you would see is a dropdown with the existing users and a dropdown with the role you want to assign to that user with a submit button that would add the role to the user_role column for that user. Do I do this with a

rails g controller add_roles new create

or

rails g scaffold add_roles

How do I get it to submit the correct info to the user table?

Was it helpful?

Solution

From my understanding, a rails scaffold is a full set of controller, model, and migration. In your case, I don't think you want a add_roles_controller, and an add_roles model, you just want to update a column of your existing Users DB correct?

If so, ask yourself if you really need a controller to do this, this type of functionality can be done in an existing user_controller (or something of the like). If you are going the CRUD route, you can consider this an update upon a user.

You can make an active record call from any controller, lets say you're in a user_controller and you have a users model, you could do something like:

@users = Users.all

That would return an object of all the user's stored in the db from which can can loop through them, picking out each individuals role attribute.

If you need help on creating a form, you're going to need to elaborate, this will require changing your routes to respond to a POST to a certain controller action. That controller action can then take the parameters of the post, say a user's role, and update the Users database accordingly

if you haven't yet, check out the gem devise - it's a very easy way to login/logout and it includes some pretty awesome session management Devise

And if you want more functionality, I'd look into rolify. I haven't used it but it seems like a great way to add roles to users. Rolify

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top