嵌套属性不保存/更新

时间:2012-12-31 15:59:54

标签: ruby-on-rails nested-attributes

我坚持这个问题。我可以为新/编辑加载视图,但创建/更新不会更改Caracteristicas的属性,也不会保存在数据库中。如果需要,Produto的属性会更新。对我而言似乎一切都在正确的位置,这就是我寻求帮助的原因。

有人可以指出应用程序的问题是无法保存/更新Caracteristica吗?

模型

class Produto < ActiveRecord::Base
  has_many :caracteristicas
  attr_accessible :titulo, :caracteristicas_attributes
  accepts_nested_attributes_for :caracteristicas, :reject_if => lambda { |c| c[:content].blank? }, :allow_destroy => true
end

class Caracteristica < ActiveRecord::Base
  belongs_to :produto
  attr_accessible :titulo, :conteudo
end

产品控制器

def new
  @produto = Produto.new
  @produto.caracteristicas.build

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @produto }
  end
end

produto.html.erb

<%= f.fields_for :caracteristicas do |builder| %>
  <%= render 'caracteristica_fields', :f => builder %>
<% end %>

caracteristica_fields.html.erb

<%= f.label :conteudo %><br />
<%= f.cktext_area :conteudo, :toolbar => 'Easy' %>

创建参数

{"utf8"=>"✓",
 "authenticity_token"=>"mnWb2X4FiolU/mPjnZcg5nA8eYUbv9GvaBawdl9jr74=",
 "produto"=>{"titulo"=>"cdsacdsacdsa",
 "caracteristicas_attributes"=>{"0"=>{"conteudo"=>"<p>\r\n\t12321312</p>\r\n"},
 "1356968992110"=>{"conteudo"=>"<p>\r\n\tdewdewdewdwe</p>\r\n"}}},
 "commit"=>"Create Produto"}

1 个答案:

答案 0 :(得分:1)

我会替换:

:reject_if => lambda { |c| c[:content].blank? }

:reject_if => lambda { |c| c[:conteudo].blank? }