Rails 3.1:嵌套属性不会通过表单保存
我怀疑这可能是一个非常简单的错误,但我花了3个小时寻找它,所以我想我可能会向社区寻求帮助。Rails 3.1:嵌套属性不会通过表单保存
我正在运行Ryan Bates在嵌套模型窗体上的优秀截屏视图,并试图将它们应用到我自己的项目中。问题是嵌套属性似乎没有使用表单保存。我可以通过控制台保存它,但只有在通过表单时才显示为空括号。
下面是相关的代码:
表单视图(使用HAML)
= form_for(@article) do |f|
- if @article.errors.any?
#error_explanation
%h2
= pluralize(@article.errors.count, "error")
prohibited this article from being saved:
%ul
- @article.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :title
%br/
= f.text_field :title
.field
= f.label :intro
%br/
= f.text_area :intro
= f.fields_for :subsections do |builder|
= render 'subsections_fields', :f => builder
.field
= f.label :published_at
%br/
= f.text_field :published_at
.actions
= submit_or_cancel(f)
subsection_fields形成视图
= f.label :header
%br/
= f.text_field :header
= f.label :order_id
= f.number_field :order_id
%br/
= f.label :body
%br/
= f.text_area :body
%br/
= f.check_box :_destroy
= f.label :_destroy, "Remove Subsection"
%br/
控制器
class ArticlesController < ApplicationController
def new
@article = Article.new
3.times { @article.subsections.build }
end
def create
@article = Article.new(params[:article])
if @article.save
flash[:notice] = "Successfully created article."
redirect_to @article
else
render :action => 'new'
end
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash[:notice] = "Successfully updated article."
redirect_to @survey
else
render :action => 'edit'
end
end
def destroy
Article.find(params[:id]).destroy
flash[:notice] = "Succesfully destroy article."
redirect_to articles_url
end
def show
@article = Article.find(params[:id])
end
def index
@articles = Article.all
end
end
,这些模型
class Article < ActiveRecord::Base
attr_accessible :title, :intro
has_many :subsections, :dependent => :destroy
accepts_nested_attributes_for :subsections, :reject_if => lambda { |a| a[:body].blank? },
:allow_destroy => true
has_and_belongs_to_many :categories
validates :title, :presence => true
end
class Subsection < ActiveRecord::Base
attr_accessible :header, :body, :order_id
belongs_to :article
validates :header, :presence => true
validates :body, :presence => true
end
任何搞清楚了这一点帮助深表感谢。
我从另一个question找出了这个答案。
答案是设置我subsections_attributes
为attr_accessible
,所以上面的文章模型应该是这样的(我还添加了published_at
作为attr_accessible
):
class Article < ActiveRecord::Base
attr_accessible :title, :intro, :subsections_attributes, :published_at
has_many :subsections, :dependent => :destroy
accepts_nested_attributes_for :subsections, :reject_if => lambda { |a| a[:body].blank? },
:allow_destroy => true
has_and_belongs_to_many :categories
validates :title, :presence => true
end
我不太确定,但在Subsection
模型中试用attr_accessible :article_id
?
添加“attr_accessible”的模式改变方式大量分配在轨道中工作。
如果您删除模型中的“attr_accessible”行,那么您的所有代码将按原样完美工作。
“accept_nested_attributes_for”类方法在模型中添加了“subsections_attributes =(value)”方法。
第二次,您将“attr_accessible”添加到模型中,现在要强制为要通过质量分配分配的每个字段添加额外的“attr_accessible”条目。即当您使用Article.new(params [:article])时。
我希望这很清楚。
我知道,当我设置模型。这个问题出现时,我认为是因为我在子模型中有'attr_accessible'方法来为每个我想要进行批量赋值的字段,所以在父模型中不需要设置任何内容。 – 2012-02-21 23:06:40
我对轨道相当陌生,但不是很危险吗?这不是允许有人做文章ID的大规模任务吗? – 2012-01-14 23:58:56
是的,这就是为什么有[MassAssignmentSecurity](http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html#method-i-attr_accessible)来帮助你分享你的属性。但是你通过形式来设置article_ids通过质量分配。 – Vapire 2012-01-15 10:21:06
这可能是一个切线有点离开,但为了我自己的理解,不要在控制器中使用'@ article.subsections.build'自动设置外键'article_id'?尽管如此,我继续尝试,但这并没有解决问题。 – 2012-01-15 22:58:08