Rails错误:无法批量分配受保护的属性:interest_ids?

js1*_*111 10 ruby-on-rails associations

我使用多步形式(使用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'
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

继承人看法:

<%= 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 %>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢!

Mis*_*cha 21

您可以通过将此错误添加到您的用户模型来消除此错误:

attr_accessible :interest_ids
Run Code Online (Sandbox Code Playgroud)

如果没有这个,interest_ids属性就会受到质量分配的保护,当你尝试为它赋值时,无论如何都会抛出异常.