Note that there are some explanatory texts on larger screens.

plurals
  1. PORails controller not rendering correct view when form is force-submitted by Javascript
    primarykey
    data
    text
    <p>I'm using Rails with jQuery, and I'm working on a page for a simple site that prints each record to a table. The only editable field for each record is a checkbox. My goal is that every time a checkbox is changed, an ajax request updates that boolean attribute for the record (i.e., no submit button).</p> <p>My view code:</p> <pre><code>&lt;td&gt; &lt;% form_remote_tag :url =&gt; admin_update_path, :html =&gt; { :id =&gt; "form#{lead.id}" } do %&gt; &lt;%= hidden_field :lead, :id, :value =&gt; lead.id %&gt; &lt;%= check_box :lead, :contacted, :id =&gt; "checkbox"+lead.id.to_s, :checked =&gt; lead.contacted, :onchange =&gt; "$('#form#{lead.id}').submit();" %&gt; &lt;% end %&gt; &lt;/td&gt; </code></pre> <p>In my routes.rb, admin_update_path is defined by</p> <pre><code> map.admin_update 'update', :controller =&gt; "admin", :action =&gt; "update", :method =&gt; :post </code></pre> <p>I also have an RJS template to render back an update. The contents of this file is currently just for testing (I just wanted to see if it worked, this will not be the ultimate functionality on a successful save)...</p> <pre><code>page &lt;&lt; "$('#checkbox#{@lead.id}').hide();" </code></pre> <p>When clicked, the ajax request is successfully sent, with the correct params, and the action on the controller can retrieve the record and update it just fine. <strong>The problem is that it doesn't send back the JS; it changes the page in the browser and renders the generated Javascript as plain text rather than executing it in-place.</strong> </p> <p>Rails does some behind-the-scenes stuff to figure out if the incoming request is an ajax call, and I can't figure out why it's interpreting the incoming request as a regular web request as opposed to an ajax request.</p> <p>I may be missing something extremely simple here, but I've kind-of burned myself out looking so I thought I'd ask for another pair of eyes. Thanks in advance for any info!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload