ruby-on-rails - Rails错误: Can't mass-assign protected attributes: interest_ids?

标签 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'

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

这是 View :
<%= 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 %>

有任何想法吗?谢谢!

最佳答案

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

attr_accessible :interest_ids

没有此属性,interest_ids属性就不会受到mass assignment的保护,并且无论如何您尝试为其分配值时都会引发异常。

关于ruby-on-rails - Rails错误: Can't mass-assign protected attributes: interest_ids?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560458/

相关文章:

node.js - 在 Sequelize 中使用嵌套关联时遇到问题

ruby-on-rails - 您如何覆盖 Rails has_one 关联的 getter 方法?

ruby-on-rails - 如何在我的 Rails View 中显示外部站点?

ruby-on-rails - 如何将文件复制到 capistrano 共享文件夹

javascript - 在另一个 View 中渲染表单,表单未显示

node.js - 在 Sequelize 中创建多对多关联的问题。 (属于多)

ruby-on-rails - 创建模型实例时如何建立 has_and_belongs_to_many 关联

ruby - 如何使用 Mongoid 在 MongoDB 中自动创建关联记录?

mysql - Mac OS X Lion 上的 MAMP 2.0.1 与 Ruby on Rails 3.2.0

ruby-on-rails - 在 Rails 中,定义常量字符串(如错误消息、通知等)的最佳方法是什么?