2011-04-02 83 views
1

不知道为什么我一直有这个问题。总之,这里是型号:另一个accep_nested_attributes_for问题 - 不删除

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 

视图(使用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 %> 

的后呼叫控制器:

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

现在,我发现虚假_destroy参数在那里结束。所以我将表单代码更改为:

<%= coupon_code.input :_destroy, :as => :select, :include_blank => false, :collection => [ ["No", 0], ["Yes", 1] ] %> 

为了解决这个问题。现在我看到:

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

但仍...没有东西被删除!

我相信这是简单的,但是,我设置allow_destroy ...还有什么需要在那里!?

回答

2

的问题是,你使用

:reject_if => proc { |attrs| attrs['code'].blank? } 

然后你发这样的数据:

{"id"=>"13", "_destroy"=>"1"} 

所以这reject_if “确认” 返回false,因为code是空白。

你需要做的是还通过了在表单code

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

,或者改变您的reject_if

:reject_if => proc { |attrs| attrs['code'].blank? && !(attrs[:_destroy] == "true" || attrs[:_destroy] == "1") } 

最后的解决方案少许清洁剂

+0

不,没什么区别。仅供参考这是一个Rails 3应用程序,运行在3.0.3 – phil 2011-04-02 12:53:22

+0

好吧,我明白了。我发现你的问题 – fl00r 2011-04-02 15:33:13

+0

谢谢!这样可行。我知道这会很愚蠢。 – phil 2011-04-03 11:00:49