我使用多步形式(使用Wicked gem)。在表单的前几个步骤中,我正在编辑用户模型,这些步骤工作正常。然后,我尝试与用户模型具有HABTM关系的“兴趣”模型。但是我得到了这个错误:
ActiveModel::MassAssignmentSecurity::Error in UserStepsController#update
Can't mass-assign protected attributes: interest_ids
Rails.root: /Users/nelsonkeating/rails_projects/Oreminder1
Application Trace | Framework Trace | Full Trace
app/controllers/user_steps_controller.rb:12:in `update'
user_steps_controller.rb
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :standard, :personal, :interests, :dates
def show
@user = current_user
render_wizard
end
def update
@user = current_user
@user.attributes = params[:user]
render_wizard @user
end
end
继承人的观点:
<%= render layout: 'form' do |f| %>
<% for interest in Interest.find(:all) %>
<label class="checkbox">
<%= check_box_tag "user[interest_ids][]", interest.id, @user.interests.include?(interest) %>
<%= interest.name %>
</label>
<% end %>
<% end %>
有什么想法吗?谢谢!
答案 0 :(得分:21)
您可以通过将此错误添加到您的用户模型中来消除此错误:
attr_accessible :interest_ids
如果没有这个,interest_ids
属性会受到mass assignment的保护,当您尝试为其赋值时,会抛出异常。