Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Auth Token and Ajax
    text
    copied!<p>Okay so from what I've read on other websites and on stack overflow, Rails throws this Authentication token error because my form doesn't pass the token -- and it's a security feature. This I understand.</p> <p>However I don't really have a form. I have ajax here -- my javascript posts the id'ed information into a processing function.</p> <p>So my question is, how do I get the authentication token to my controller?</p> <p>My view looks like this: </p> <pre><code>&lt;% for transaction in @transactions %&gt; &lt;% if transaction["category"] == '' %&gt; &lt;% transaction["category"] = "Uncategorized" %&gt; &lt;% end %&gt; &lt;tr title = "&lt;% if params[:type] %&gt;&lt;%= params[:type] %&gt;&lt;% else %&gt;Purchases&lt;% end %&gt; &lt;%= transaction["id"] %&gt;" &gt; &lt;td class="check"&gt;&lt;a class="help" href="#"&gt;&lt;img src="/images/icons/help.png" alt="?" /&gt;&lt;/a&gt;&lt;input type="checkbox" /&gt;&lt;/td&gt; &lt;td class="date"&gt;&lt;% if transaction["date"] != "0000-00-00 00:00:00" %&gt;&lt;%= transaction["date"].to_date.strftime("%B %d") %&gt;&lt;% end %&gt;&lt;/td&gt; &lt;% if params[:type] == "Bills" || params[:type] == "Reimbursements" %&gt; &lt;td class="payee"&gt;&lt;%= transaction["payee"] %&gt;&lt;/td&gt; &lt;td class="details"&gt;&lt;%= transaction["details"] %&gt;&lt;/td&gt; &lt;% else %&gt; &lt;td class="description"&gt;&lt;% if transaction["detail"] == "undefined" %&gt;n/a&lt;% else %&gt;&lt;%= transaction["detail"] %&gt;&lt;% end %&gt;&lt;/td&gt; &lt;td class="category"&gt;n/a&lt;/td&gt; &lt;% end %&gt; &lt;td class="amount"&gt;-$&lt;%= transaction["amount"] %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p>The corresponding ajax is as follows:</p> <pre><code>/* send ids by ajax */ $('#tableActions li a').click(function() { if(!$(this).hasClass('disabled')) { action = $(this).text(); ids = new Array(); i = 0; $('td.check input','#tableHolder').each(function() { if($(this).attr('checked')) { ids[i++] = $(this).parents('tr').attr('title'); } }); $.ajax({ type: "POST", url: "/bulkaction", data: "=" + action + "&amp;ids=" + ids + "&amp;authenticity_token=" + encodeURIComponent(AUTH_TOKEN), success: function(data){ $('#tableHolder').html(data); /* bring back all functionality */ initTable(); /* set default sorting by date desc */ $('th').removeClass('sortUp sortDown'); $('th:eq(1)').addClass('sortDown'); /* disable all actions */ $('#tableActions li a').addClass('disabled'); } }); } return false; }); </code></pre> <p>My processing logic in the controller looks like</p> <pre><code> def bulkaction if request.post? ids = params[:ids] #Need to create a function here to parse out my string puts ids #for testing purposes, just put my ids onto the console end puts "This function was accessed and ran." end </code></pre> <p>And finally the console says</p> <pre><code>Processing UserController#bulkaction (for ::ffff:xx.xxx.xxx.xxx at 2009-07-06 23 :29:49) [POST] Parameters: {"ids"=&gt;"Purchases 10040963"} ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticit yToken): /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `each' /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start' </code></pre> <p>It would be very helpful if someone were able to tell me where I was going wrong.</p>
 

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