RSL
RSL
besides, thanks to your advice I've shortened this up to
def reason_attributes=(reason_attributes)
reasons.clear
reason_attributes.uniq.each do |attributes|
reason = Reason.find_or_initialize_by_content(attributes)
if self.new_record?
# Entering create.
reasons << reason # no matter if it's invalid
else
# Entering update.
if reason.valid?
reasons << reason
else
reasons.build(attributes) # workarounded to show error
messages
end
end
end
end
do you have further optimisation in mind or I'm missing something
obvious?
Il giorno 01/set/08, alle ore 14:29, Russell Norris ha scritto:
doesn't make sense to me. the first line already finds or creates a
reason. if it raises an exception adding it to the reasons collection,
i don't see why building a new one with the exact same information
would solve anything. you've tried this?
RSL
# _report.erb
<div class="reason">
<dl class="form">
<% fields_for("report[reason_attributes][]", reason) do |r| %>
<dt class="required">Reason <%= r.error_message_on :content %></dt>
<dd>
<%= r.text_field :content, { :index => nil, :autocomplete =>
'off' } %>
<%= link_to_function "remove", "$(this).up('.reason').remove()" %>
</dd>
<% end %>
</dl>
</div>
# edit.erb
<% form_for [:administration, @document, @report] do |f| -%>
...
<div id="reasons">
<%= render :partial => 'reason', :collection => @report.reasons %>
</div>
<p><%= add_reason_link "Add a reason" %></p>
<%= f.submit %>
<% end -%>
# controller update action uses only if
@report.update_attributes(params[:report])
when you first access the edit page, _report is cycled through every
reason it finds, and so it's ok.
the moment I edit a reason field, putting in there an invalid
attribute that doesn't pass the validation I need to rescue the
exception thrown by << somewhere.
I decided to rescue this exception with a reasons.build(attributes),
which will instantiate a new reason on the invalid field, thus showing
the error message.
Il giorno 01/set/08, alle ore 15:06, Russell Norris ha scritto: