Reject_if not working
I have some troubles with :reject_if
. I don't know why the following code doesn't work.
View - _form.html.erb:
<%= f.fields_for :questions do |builder| %>
<div class="nested-field">
<%= builder.label :id, "Question" %><br />
<%= builder.collection_select(:id, Question.all(:order => 'question'), :id, :question, { :prompt => 'Select Question' } ) %>
</div>
<div class="nested-field">
<%= builder.label :test1 %><br />
<%= builder.text_field :test1 %>
</div>
<div class="nested-field">
<%= builder.label :test2 %><br />
<%= builder.text_field :test2 %>
</div>
<div class="nested-field">
<%= builder.label :description %><br />
<%= builder.text_field :description 开发者_高级运维%>
</div>
<br /><br /><br />
<hr />
<% end %>
Model - questionary.rb:
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:id].blank? }, :allow_destroy => true
Thank you very much!
Instead of
:reject_if => lambda { |a| a[:id].blank? }
try
:reject_if => lambda { |a| a['id'].blank? }
(note the string 'id') and to follow the API exactly, use proc
:reject_if => proc { |a| a['id'].blank? }
I had a look at the API for accepts_nested_attributes_for, and found there that the documentation states:
:reject_if Allows you to specify a Proc or a Symbol pointing to a method that checks whether a record should be built for a certain attribute hash.
Have you tried to replace the lambda with proc? The syntax here seems to be special, so the lambda may be just ignored.
精彩评论