Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax submissions with jquery in rails not working
    primarykey
    data
    text
    <p>I've been trying to implement a commenting system for my Rails app. Every event in my app has it's own page, and on the page is a comments thread. I want users to be able to submit comments using ajax. I have tried to follow RailsCast 136, which is basically what I want to have work in my app. However, whenever I post a comment, the ajax request is not going through, and I'm receiving the error:</p> <blockquote> <p>Started POST "/events/undefined" for 127.0.0.1 at 2011-07-17 00:39:16 -0400</p> <p>ActionController::RoutingError (No route matches "/events/undefined"):</p> </blockquote> <p>Here's the code for my show action in my events_controller. </p> <blockquote> <p>def show</p> <pre><code>@event = Event.find(params[:id]) @comment = Comment.new session[:event_id] = @event.id @comments = @event.comments.reverse end </code></pre> </blockquote> <p>Here's the code for my comment form in my event show view. </p> <blockquote> <p> &lt;%= form_for(@comment, :remote => true) do |f| %> &lt;% if signed_in? %> <p>&lt;%= f.label :author %><br /> &lt;%= f.text_field :author, :value => current_user.name %></p> &lt;% else %> <p>&lt;%= f.label :author %><br /> &lt;%= f.text_field :author %></p><br> &lt;% end %></p> <pre><code> &lt;p&gt;&lt;%= f.label :content %&gt;&lt;br /&gt; &lt;%= f.text_field :content %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.submit %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> </blockquote> <p>Here's the code for my create action in my comments_controller. </p> <blockquote> <pre><code>def create @comment = Comment.create!(params[:comment]) @comment.event_id = session[:event_id] session[:event_id] = nil flash[:success] = "Comment sent!" respond_to do |format| format.html { redirect_to @comment.event } format.js end end </code></pre> </blockquote> <p>Here's the jQuery code for posting with ajax. It is pretty much the same as the code outlined by RailsCast 136</p> <blockquote> <p>jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} })</p> <p>$(document).ready(function(){ $("#commentform").submit(function(){ $.post($(this).attr("action"), $(this).serialize(), null, "script"); return false; }) })</p> </blockquote> <p>Anybody have any idea what could be going wrong? I'm very new to Rails and web development in general and appreciate any help fixing this.</p> <p>EDIT: I managed to fiddle around and fix the problem. I essentially followed the solution that vinceh laid out in addition to nesting my routes and got it to work. Thanks guys.</p> <p>Thanks so much, Sid</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.
    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