Wrong number of arguments (2 for 1) Rails 4. Perhaps the problem is strong_params

I am using Rails 4 with Devise, Cancan and Rollify.

I have a modal change user index. However, when I try to update the role, I get the following error: "Wrong number of arguments (2 for 1)"

The error is on line 16 of my user controller code:

13 def update 14 authorize! :update, @user, message: 'Not authorized as an administrator.' 15 @user = User.find(params[:id]) 16 if @user.update_attributes(params[:user], as: :admin) 17 redirect_to users_path, notice: "User updated." 18 else 19 redirect_to users_path, alert: "Unable to update user." 20 end 21 end 

A form that submits parameters:

 <div id="role-options-<%= user.id %>" class="reveal-modal medium" style="display: none;"> <%= simple_form_for user, url: user_path(user), html: {method: :put, class: 'custom' } do |f| %> <h3>Change Role</h3> <%= f.input :role_ids, collection: Role.all, as: :radio_buttons, label_method: lambda {|t| t.name.titleize}, label: false, item_wrapper_class: 'inline', checked: user.role_ids.first %> <%= f.submit "Change Role", class: "small button" %> <a class="close-reveal-modal" href="#">Close</a> <% end %> </div> 

Here is my role model:

 class Role < ActiveRecord::Base has_and_belongs_to_many :users, :join_table => :users_roles belongs_to :resource, :polymorphic => true scopify end 

I assume this has something to do with the change from attr_accessible to Strong Paramenters in Rails 4, but I'm not sure. If so, where can I put the private method?

+4
source share
2 answers

This line contains an error, because update_attributes accepts only one parameter, you are trying to pass 2 parameters. I suggest you pass your second parameter, merging with the params[:user] hash.

it should be:

 if @user.update_attributes(params[:user]) 

instead:

 if @user.update_attributes(params[:user], as: :admin) 

Hope this helps. thanks

+2
source

I ran into the same problem. And my solution creates a private method in the User controller and changes the update settings.

 private def user_params params.require(:user).permit! end 

then

 if @user.update_attributes!(user_params) 

This works great with me!

+2
source

Source: https://habr.com/ru/post/1495111/


All Articles