Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:25:07+00:00 2026-05-23T19:25:07+00:00

I’ve been trying to implement a commenting system for my Rails app. Every event

  • 0

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:

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:25:09+00:00Added an answer on May 23, 2026 at 7:25 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I'm looking for suggestions for debugging... If you view this site in Firefox or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.