简体   繁体   中英

wrong number of arguments (2 for 1) Rails 4. Maybe strong_params issue

I'm using Rails 4 with Devise, Cancan and Rollify.

I've got an index of users with a modal to change the role. However when I try to update the role I get the following error: "wrong number of arguments (2 for 1)"

The error occurs in 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

The form that is sending the params is:

<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's 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'm guessing that it has something to do with the change from attr_accessible to Strong Paramenters in Rails 4 but I'm not sure. If it is, where do I put the private method?

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

it should be :

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

in place of:

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

Hope it will help.Thanks

I encountered the same problem. And my solution is creating a private method in User controller and changed the update params.

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

then

if @user.update_attributes!(user_params)

This works fine with me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM