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 715459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:11:22+00:00 2026-05-14T05:11:22+00:00

As I always type I am new to rails and programming in general so

  • 0

As I always type I am new to rails and programming in general so go easy. Thanks in advance.

I have successfully followed the initial tutorial from Ryan Bates on how to build a weblog in 15 minutes. If you don’t know this tutorial takes you through creating posts and allowing for comments on those post. It even introduces AJAX through the creating and displaying comments on the posts show.html.erb page. All works great.

Here’s the hiccup, when Ryan takes you though this tutorial he clears out the comments_controller and only shows the code for creating comments. I am trying to add back the ability to edit and destroy comments. Can’t seem to get it to work, keeps deleting the actual post not the comment (log shows that I keep sending DELETE request to PostsController). Here is my code:

class CommentsController < ApplicationController
 def create
   @post = Post.find(params[:post_id])
   @comment = @post.comments.create!(params[:comment])
   respond_to do |format|
     format.html { redirect_to @post }
     format.js
   end
 end

 def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml  { head :ok }
    end
  end
end

/views/posts/show.html.erb

    <%= render :partial => @post %>

    <p>
        <%= link_to 'Edit', edit_post_path (@post) %> |
        <%= link_to 'Destroy', @post, :method => :delete, :confirm => "Are you sure?" %> |
        <%= link_to 'See All Posts', posts_path %>
    </p> 

    <h2>Comments</h2>
    <div id="comments">
        <%= render :partial => @post.comments %>
    </div>

    <% remote_form_for [@post, Comment.new] do |f| %>
        <p>
            <%= f.label :body, "New Comment" %><br/>
            <%= f.text_area :body %>
        </p>
        <p>

<%= f.submit "Add Comment" %></p>
<% end %>

/views/comments/_comment.html.erb

<% div_for comment do %>
    <p>
        <strong>Posted <%= time_ago_in_words(comment.created_at) %> ago
        </strong><br/>
        <%= h(comment.body) %><br/>
        <%= link_to 'Destroy', @comments, :method => :delete, :confirm => "Are you sure?" %>
    </p>
<% end %>

routes.rb

ActionController::Routing::Routes.draw do |map|
  map.resources :posts, :has_many => :comments
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end
  • 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-14T05:11:23+00:00Added an answer on May 14, 2026 at 5:11 am

    meagar is on the right path, but since this is a nested route you have to do:

    <%= link_to 'Destroy', [@post, comment], ... %>

    So, you are passing the comment and the post and letting rails figure out the route based on your definitions.

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

Sidebar

Related Questions

I'm downloading some images from a service that doesn't always include a content-type and
My scenario should be simple... the type I want to convert FROM is ALWAYS
One may not always know the Type of an object at compile-time, but may
I've personally always used some type of external application for my SCM work; these
I always tell in c# a variable of type double is not suitable for
I always see the code like this in the blogs: $.ajax({ type: POST, contentType:
Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort()
Always when I run java application it will display in Windows Task Manager is
I always believe they did, but seeing some answers here make me doubt... Can
I always tend to forget these built-in Symfony functions for making links.

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.