Rails 3.1:嵌套属性不会通过表单保存

时间:2012-01-14 21:00:11

标签: ruby-on-rails ruby-on-rails-3 nested-attributes

我怀疑这可能是一个非常简单的错误,但我花了3个小时寻找它,所以我想我可能会向社区寻求帮助。

我正在浏览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

非常感谢任何帮助解决这个问题。

3 个答案:

答案 0 :(得分:1)

我不太确定,但是在您的attr_accessible :article_id模型中也可以使用Subsection进行尝试吗?

答案 1 :(得分:1)

将“attr_accessible”添加到模型会改变质量分配在rails中的工作方式。

如果删除模型中的“attr_accessible”行,那么所有代码​​都可以完美地运行。

类方法“accepts_nested_attributes_for”将“subsections_attributes =(value)”方法添加到模型中。

您向模型添加“attr_accessible”的第二个,您现在被迫为要通过质量分配分配的每个字段添加额外的“attr_accessible”条目。即当你使用Article.new(params [:article])时。

我希望这很清楚。

答案 2 :(得分:0)

我从另一个question找到了这个问题的答案。

答案是将我的subsections_attributes设置为attr_accessible,因此上面的文章模型应如下所示(我还将published_at添加为attr_accessible):< / p>

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