gpt4 book ai didi

ruby-on-rails - 无法批量分配 protected 属性以使用设计创建 has_many 嵌套模型

转载 作者:行者123 更新时间:2023-12-04 08:35:50 25 4
gpt4 key购买 nike

我看过 RailsCast,另一个嵌套属性视频,很多 SO 帖子,并为此斗争了一段时间,但我仍然无法弄清楚。我希望它是很小的东西。

我有两个型号,User (由 Devise 创建)和 Locker (又名,产品愿望 list ),我正在尝试创建一个 Locker对于 User当他们注册时。我的登录表单有一个字段,用于输入他们的新名称 Locker (恰本地称为 :name ),我试图分配给在新用户注册时创建的储物柜。我收到的所有问候是:
WARNING: Can't mass-assign protected attributes: locker
我已经尝试了 accepts_nested_attributes 的所有组合和 attr_accesible在我的两个模型中,仍然没有任何效果。我可以从日志中看到它正在由 Devise#create 方法处理,而且我知道 Devise 不够聪明,无法按照我想要的方式创建模型:)

这是我的两个模型的相关部分:

# user.rb    
class User < ActiveRecord::Base
attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :locker_attributes

# Associations
has_many :lockers
has_many :lockups, :through => :lockers

# Model nesting access
accepts_nested_attributes_for :lockers
end


# locker.rb
class Locker < ActiveRecord::Base
belongs_to :user
has_many :lockups
has_many :products, :through => :lockups

attr_accessible :name, :description
end

# lockers_controller.rb (create)
@locker = current_user.lockers.build(params[:locker])
@locker.save

我假设我需要覆盖 Devise 的 create以某种方式使其工作的方法,但我对 rails 很陌生,并且已经习惯了这一切的黑匣子“魔法”性质。

如果有人能帮助我,我会非常感激。已经在这上面花费了太多时间:)

编辑:我意识到我在问题中遗漏了一些东西。我的 Locker模型具有三个属性 - name , description (非强制性)和 user_id将其链接回 User .我的注册表单只需要 name ,所以我不会遍历嵌套表单中的所有属性。这也和我的问题有关吗?

编辑 2:我还想出了如何覆盖 Devise 的 RegistrationsController#create方法,我只是不知道该放什么。设计的整体 resource事情对我来说没有意义,浏览他们的 RegistrationsController 的源代码也没有帮助我。

对于奖励积分:当用户提交包含无效数据的登录表单时, Locker字段总是返回空白,而常规的设计字段、用户名和电子邮件则被填充。这也可以轻松修复吗?如果是这样,如何?

最佳答案

首先,你有一个错字:

attr_accessible :locker_attributes

应该是复数:
attr_accessible :lockers_attributes

然后,标准的使用方式 nested_attributes是 :
<%= form_for @user do |f| %>
<%# fields_for will iterate over all user.lockers and
build fields for each one of them using the block below,
with html name attributes like user[lockers_attributes][0][name].
it will also generate a hidden field user[lockers_attributes][0][id]
if the locker is already persisted, which allows nested_attributes
to know if the locker already exists of if it must create a new one
%>
<% f.fields_for :lockers do |locker_fields| %>
<%= locker_fields.label :name %>
<%= locker_fields.text_field :name %>
<% end %>
<% end %>

在你的 Controller 中:
def new
@user = User.new
@user.lockers.build
end

def create
# no need to use build here : params[:user] contains a
# :lockers_attributes key, which has an array of lockers attributes as value ;
# it gets assigned to the user using user.lockers_attributes=,
# a method created by nested_attributes
@user = User.new( params[:user] )
end

作为旁注,您可以通过不同的方式避免在 Controller 中为新用户构建新的储物柜:
  • User 上创建工厂方法,或覆盖 new ,或使用 after_initialize回调以确保实例化的每个新用户都会获得自动构建的储物柜
  • 将特定对象传递给 fields_for :
    <% f.fields_for :lockers, f.object.lockers.new do |new_locker_fields| %>
  • 关于ruby-on-rails - 无法批量分配 protected 属性以使用设计创建 has_many 嵌套模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16746998/

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