gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:59:21 25 4
gpt4 key购买 nike

我正在处理多步骤表单(使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com