与nested_attributes的另一个质量分配错误

时间:2012-10-22 19:30:45

标签: ruby-on-rails nested-attributes

我不确定为什么在尝试使用复选框更新用户位置的布尔属性时,我仍然会收到此批量分配错误(布尔属性为“is_visible”):

以下是质量分配错误:

'Can't mass-assign protected attributes: position'

用户has_many职位。 Position具有属性“is_visible”,它是布尔值。

你会注意到我在我的用户模型中包含了“accepts_nested_attributes_for”。我还在我的用户模型“positions_attributes”中包含了一个可访问的属性。

我的用户模型的摘录:

class User < ActiveRecord::Base

attr_accessible :position_ids, :positions_attributes

has_many :positions, dependent: :destroy
accepts_nested_attributes_for :positions

end

我的位置模型:

class Position < ActiveRecord::Base
  attr_accessible :is_visible

  belongs_to :user  
end

摘自我的观点:

<%= form_for(@user) do |f| %>
  <% @positions.present? and @positions.each do |position| %>
    <%= f.fields_for position do |p| %>
      <%= p.check_box :is_visible %>
    <% end %>
    Make this positions visible
<% end %>

现在来自我的用户控制器:

  def edit_profile
    @user = current_user
    @preferred_venue = @user.preferred_venues.last if @user.preferred_venues
    @positions = @user.positions
    @educations = @user.educations
  end

  def update
    @user = User.find(params[:id])

    if @user.update_attributes(params[:user])
      flash[:success] = "Profile saved"
      redirect_to @user
    end

  end

非常感谢任何反馈/帮助。谢谢!

0 个答案:

没有答案
相关问题