簡體   English   中英

錯誤的參數數量(2對1)Rails 4.也許strong_params問題

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

我正在使用Rails 4與Devise,Cancan和Rollify。

我有一個用戶的索引,有一個模態來改變角色。 但是,當我嘗試更新角色時,我收到以下錯誤:“錯誤的參數數量(2個為1)”

錯誤發生在我的用戶控制器代碼的第16行:

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

發送params的表單是:

<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>

這是我的角色模型:

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

  scopify
end

我猜它與Rails 4中從attr_accessible到Strong Paramenters的變化有關,但我不確定。 如果是,我在哪里放私人方法?

此行包含錯誤,因為update_attributes只接受一個參數,您嘗試傳遞2個參數。 我建議你傳遞你的第二個參數與你的params[:user]哈希合並。

它應該是 :

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

取代:

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

希望它會有所幫助。謝謝

我遇到了同樣的問題。 我的解決方案是在用戶控制器中創建一個私有方法並更改更新參數。

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

然后

if @user.update_attributes!(user_params)

這對我很好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM