Rails Add a rotation role through a user form

I am creating an application based on Devise, CanCan and Rolify for processing private resources.

The account is disabled, because I do not want users to register, and I create an administrator interface for adding / editing users.

Now I am dealing with the user role. I would like to allow the administrator to create users and set their role using the same form. I don’t know what relation rollify uses, because I'm pretty new to rails and Ruby and add a method rolifyinstead of a clearer relation.

I currently need and want to have only one role for each user. Therefore, if someone edits a user changing a role, I delete everything @user.rolesand then install a new one.

I am currently working using a simple hack. I added a form field to the form using a collection Roleto provide a selection of roles. Then inside the methods createand updateI do this:

role = params[:user][:role]
params[:user].delete :role
@user = User.new(params[:user])

respond_to do |format|
  if @user.save # update_attributes inside update
    @user.add_role role
    # ...
  end
end

As you can see, I do not interact with the model Roledirectly, but using the provided method add_role. This also creates another problem because I need to get the current role so that the update view sets the correct default value for the selection (besides the most important fact that I don't know how to check for ONE role).

, , , - , . : - (

- , .

P.S. , , users_roles, , . !!

+5
1

Rolify. , , , , , , has_role? CanCan.

, (= ).

, SO 30 , , .

.

+2

All Articles