Ajax submissions with jquery in rails not working
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开发者_StackOverflow I post a comment, the ajax request is not going through, and I'm receiving the error:
Started POST "/events/undefined" for 127.0.0.1 at 2011-07-17 00:39:16 -0400
ActionController::RoutingError (No route matches "/events/undefined"):
Here's the code for my show action in my events_controller.
def show
@event = Event.find(params[:id])
@comment = Comment.new
session[:event_id] = @event.id
@comments = @event.comments.reverse
end
Here's the code for my comment form in my event show view.
<div id="commentform"> <%= form_for(@comment, :remote => true) do |f| %>
<% if signed_in? %>
<p><%= f.label :author %><br />
<%= f.text_field :author, :value => current_user.name %></p>
<% else %>
<p><%= f.label :author %><br />
<%= f.text_field :author %></p>
<% end %>
<p><%= f.label :content %><br />
<%= f.text_field :content %></p>
<p><%= f.submit %></p> <% end %> </div>
Here's the code for my create action in my comments_controller.
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
Here's the jQuery code for posting with ajax. It is pretty much the same as the code outlined by RailsCast 136
jQuery.ajaxSetup({ 'beforeSend': function(xhr)
{xhr.setRequestHeader("Accept", "text/javascript")} })
$(document).ready(function(){ $("#commentform").submit(function(){
$.post($(this).attr("action"), $(this).serialize(), null, "script");
return false; }) })
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.
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.
Thanks so much, Sid
I see 2 things right on top. First you are not specifying where the form is sent to. So you need to change that to
<%= form_for @comment, :url => {:controller => :comments, :action => :create}, :remote => true do |f| %>
<% if signed_in? %>
<%= f.label :author %>
<%= f.text_field :author, :value => current_user.name %>
<% else %>
<%= f.label :author %>
<%= f.text_field :author %>
<% end %>
<p><%= f.label :content %><br />
<%= f.text_field :content %></p>
<p><%= f.submit %></p>
<% end %>
Then you need to add the appropriate route in your routes.rb
folder, ie
match "comments/create" => "comments#create"
See if that produces something new. Let me know and I'll keep updating this answer.
If you want a good comments AJAX tutorial, read this!
Good luck!
Based on your comment on vinceh's answer, I'm wondering if you could make things easier on yourself by nesting your resources properly in your routes.rb. Modify your existing :events route to provide a block, like so:
resources :events do
resources :comments, :only [:create]
end
Then, if your form still isn't working, try this:
<%= form_for(@comment, :url => event_comments_url(@event), :remote => true) do |f| %>
This way, it will automatically pick up the :event_id in the params as part of the post.
精彩评论