我想要accept_nested_attributes_for,但它沒有正確拒絕。這裏有兩種型號:accep_nested_attributes_for - 如何拒絕空巢?
投票
class Poll < ActiveRecord::Base
has_many :poll_options, :dependent => :destroy
accepts_nested_attributes_for :poll_options, :reject_if => proc { |attributes| attributes['title'].blank? }
POLLOPTION
class PollOption < ActiveRecord::Base
belongs_to :poll
我建立與3個poll_options民意調查的形式,但如果你填寫2 3中, 3rd沒有被刪除。這裏的日誌:
Started POST "/polls/50" for 127.0.0.1 at Sun Jun 26 16:31:52 -0700 2011
Processing by PollsController#update as JS
Parameters: {"poll"=>{"title"=>"Poll Options (2 only).3", "poll_options_attributes"=>{"0"=>{"title"=>"AAA", "id"=>"145"}, "1"=>{"title"=>"BBBB", "id"=>"146"}, "2"=>{"title"=>"", "id"=>"147"}}}, "commit"=>"Publish", "authenticity_token"=>"KaQrzinP3GNey9zN+sc0vAWU+VeUX1TRFSnQSscW7IA=", "utf8"=>"✓", "id"=>"50"}
控制器
@poll = Poll.new(:user_id => current_user)
3.times do
option = @poll.poll_options.build
end
表
<%= form_for(@poll, :remote => true) do |f| %>
<%= f.text_field :title %>
<%= f.fields_for :poll_options do |f| %>
<%= f.text_field :title %>
<% end %>
<%= f.submit :class => '', :value => 'Publish' %>
<% end %>
任何想法,我做錯了嗎?謝謝
更新與控制器和表單代碼。 – AnApprentice
需要更多的細節? – AnApprentice