0

我被困在这个问题上。我可以为新/编辑加载视图,但创建/更新不会更改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

产品.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"}
4

1 回答 1

1

我会替换:

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

:reject_if => lambda { |c| c[:conteudo].blank? }
于 2012-12-31T16:25:45.350 回答