another accepts_nested_attributes_for issue - not deleting
Not sure why I keep having issues with this. Anyway, here are the models:
class Coupon < ActiveRecord::Base
has_many :coupon_codes, :dependent => :destroy
accepts_nested_attributes_for :coupon_codes, :allow_destroy => true,
:reject_if => proc { |attrs| attrs['code'].blank? }
end
class CouponCode < ActiveRecord::Base
belongs_to :coupon
end
The view (using formtastic):
<%= semantic_form_for [:admin, @coupon] do |f| %>
<%= f.inputs "Codes" do %>
<%= f.semantic_fields_for :coupon_codes do |coupon_code| %>
<% unless coupon_code.object.new_record? %>
<%= coupon_code.input :_destroy, :as => :boolean %>
<% else %>
<%= coupon_code.input :code %>
<% end %>
<% end %>
<% end %>
<%= f.buttons %>
<% end %>
The post call to the controller:
Started POST "/admin/coupons/12" for 127.0.0.1 at Sat Apr 02 14:18:47 +0200 2011
Processing by Admin::CouponsController#update as HTML
Parameters: {"commit"=>"Update Coupon", "coupon"=>{"name"=>"Test2",
"coupon_codes_attributes"=>{"0"=>{"id"=>"13"}, "1"=>{"code"=>"Silly4"}},
"available"=>"true", "num_free_months"=>"0", "auto_generate_codes"=>"false",
"num_codes"=>"", "num_discount_months"=>"999", "discount_type"=>"value",
"code_prefix"=>"", "users_per_code"=>"10", "monthly_discount"=>"5.0"},
"authenticity_token"=>"f2lPUs2bsDBwlTtNJP63+gnub04nz8PR/+NKzs7ZY9Y=", "utf8"=>"✓",
"id"=>"12", "_destroy"=>"0"}
Now, I noticed a spurious _destroy parameter at the end there. So I changed the form code to:
<%= coupon_code.input :_destroy, :as => :select, :include_blank => false, :collection => [ ["No", 0], ["Yes", 1] ] %>
To get around that bug. And now I see:
Started POST "/admin/coupons/12" for 127.0.0.1 at Sat Apr 02 14:25:56 +0200 2011
Processing by Admin::CouponsController#update as HTML
Parameters: {"commit"=>"Update Coupon", "coupon"=>{"name"=>"Test2",
"coupon_codes_attributes"=>{"0"=>{"id"=>"13", "_destroy"=>"1"}, "1"=>{"id"=>"14",
"_destroy"=>"0"}, "2"=>{"code"=>""}}, "available"=>"true", "num_free_开发者_如何学Gomonths"=>"0",
"auto_generate_codes"=>"false", "num_codes"=>"", "num_discount_months"=>"999",
"discount_type"=>"value", "code_prefix"=>"", "users_per_code"=>"10",
"monthly_discount"=>"5.0"},
"authenticity_token"=>"f2lPUs2bsDBwlTtNJP63+gnub04nz8PR/+NKzs7ZY9Y=", "utf8"=>"✓",
"id"=>"12"}
But still... nothing gets deleted!
I am sure it is something simple, but, I am setting allow_destroy... what else needs to be there!?
THe issue is that you use
:reject_if => proc { |attrs| attrs['code'].blank? }
And then you send this data:
{"id"=>"13", "_destroy"=>"1"}
so this reject_if
"validation" return false, because code
is blank.
What you need is to also pass code
in your form:
<% unless coupon_code.object.new_record? %>
<%= coupon_code.input :_destroy, :as => :boolean %>
<%= coupon_code.input :code, :as => :hidden %> # or how it use hidden field with formastic
<% else %>
<%= coupon_code.input :code %>
<% end %>
or to change your reject_if
:reject_if => proc { |attrs| attrs['code'].blank? && !(attrs[:_destroy] == "true" || attrs[:_destroy] == "1") }
last solution little cleaner
精彩评论