I''m trying to write an interface for editing many-to-one relationships
with AJAX. Picture the to-do editor in Backpack (and, to a lesser
extent, Basecamp) and you''ll have a good idea of what I''m
trying
to do.
I''ve almost got everything working: the ''edit'' link
hides the stored
data and replaces it with a form, and the data is updated in the
database when the form is submitted. For some reason, however,
after the record is saved, the list is updated with a NoMethodError
message instead of with the new data. If I hit refresh on the page,
the list displays with the updated data (but that obviously defeats
the point of using AJAX).
For context, see snippets from the relevant files and the actual
error message below. I feel like the error must be in my
form_remote_tag call, but I''m not sure what it is. The
relationship is Contract has many Policies.
Thanks in advance for helping me sort this out.
AJS
./app/views/contracts/show.rhtml
==<div id="insurance-policies">
<h2>Insurance Policies</h2>
<ul id="contract-insurance-policy-list">
<%= render :partial => "policy", :collection => @policies
%>
</ul>
</div>
./app/views/contracts/_policy.rhtml
== <li id="insurance-<%= fmt_policy_id(policy.policytype)
%>-info">
<!-- *snip* display policy info. this works -->
<a
href="javascript:Element.toggle(''insurance-<%fmt_policy_id(policy.policytype)
%>-info'');Effect.Appear(''insurance-<%fmt_policy_id(policy.policytype)
%>-edit'');">edit</a> <!-- this also
works -->
</li>
<li id="insurance-<%= fmt_policy_id(policy.policytype)
%>-edit"
style="display:none;">
<!--[form:insurance-<%= fmt_policy_id(policy.policytype)
%>-form]-->
<%= form_remote_tag(:url => { :action =>
"update_policy_info" },
:update =>
"contract-insurance-policy-list",
:position => :bottom,
:complete =>
"Element.hide(''insurance-#{fmt_policy_id(policy.policytype)}-edit'');new
Effect.highlight(''insurance-#{fmt_policy_id(policy.policytype)}-info'');")
%>
<input name="policy_active" type="checkbox" />
<input name="policy_id" type="hidden"
value="<%= policy.id %>" />
<%= policy.policytype %>
expires <%= select_date policy.exp_date %>
<%= submit_tag "Update" %>
<a
href="javascript:Element.toggle(''insurance-<%fmt_policy_id(policy.policytype)
%>-info'', ''insurance-<%fmt_policy_id(policy.policytype)
%>-edit'');">cancel</a>
<%= end_form_tag %>
<!--[eoform:insurance-<%= fmt_policy_id(policy.policytype)
%>-form]-->
</li>
./app/controllers/contracts_controller.rb
== def update_policy_info
@policy = Policy.find(@params[:policy_id])
# *snip* Updating the policy works
if @policy.save
render :partial => :policy, :object => @policy
end
end
And the actual error:
== NoMethodError in Contracts#update_policy_info
undefined method `include?'' for :policy:Symbol
/app/controllers/contracts_controller.rb:93:in `update_policy_info''
script/server:49
Show framework trace
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_view/partials.rb:80:in
`partial_pieces''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_view/partials.rb:50:in
`render_partial''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:618:in
`render_with_no_layout''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/layout.rb:216:in
`render_without_benchmark''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:25:in
`render''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:25:in
`measure''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:25:in
`render''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:756:in
`send''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:756:in
`perform_action_without_filters''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/filters.rb:295:in
`perform_action_without_benchmark''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in
`perform_action_without_rescue''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in
`measure''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in
`perform_action_without_rescue''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/rescue.rb:80:in
`perform_action''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:356:in
`send''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:356:in
`process''
c:/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/dispatcher.rb:32:in
`dispatch''
c:/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:105:in
`handle_dispatch''
c:/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:71:in
`service''
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service''
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run''
c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread''
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start''
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread''
c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start''
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each''
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start''
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start''
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start''
c:/ruby/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:57:in
`dispatch''