- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看过 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
create
以某种方式使其工作的方法,但我对 rails 很陌生,并且已经习惯了这一切的黑匣子“魔法”性质。
Locker
模型具有三个属性 -
name
,
description
(非强制性)和
user_id
将其链接回
User
.我的注册表单只需要
name
,所以我不会遍历嵌套表单中的所有属性。这也和我的问题有关吗?
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 %>
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
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/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!