waldyr.ar
waldyr.ar

Reputation: 15244

Nested attributes not saving/updating

I'm stuck on this problem. I'm able to load view for new/edit but create/update don't change Caracteristicas's attributes nor save in database. The Produto's attributes are updated if needed. For me it seems to be all in the correct place, that's why I'm asking for help.

Can someone point the application's problem for not being able to save/update Caracteristica?

Models

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

Produto controller

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' %>

create params

{"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"}

Upvotes: 0

Views: 327

Answers (1)

apneadiving
apneadiving

Reputation: 115541

I'd replace:

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

with

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

Upvotes: 1

Related Questions