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

房东的猫 提交于 2019-12-23 12:46:55

问题


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?


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/18034494/wrong-number-of-arguments-2-for-1-rails-4-maybe-strong-params-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!