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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:52:24+00:00 2026-06-15T04:52:24+00:00

I found plenty of example on how to construct a multi-model form and multi-model

  • 0

I found plenty of example on how to construct a multi-model form and multi-model display. But what if I want to have separate forms and displays?

post.rb:

class Post < ActiveRecord::Bas
  has_many :comments, dependent: :destroy
  attr_accessible :comments_attributes
  accepts_nested_attributes_for :comments
end

comment.rb:

class Comment < ActiveRecord::Base
  belongs_to :post
end

posts_controller.rb:

def new
  @post = Post.new
  @post.comments.build
  ...
end

routes.db:

resources posts do
  resources comments
end

I have a link to post comments index in my post index:

views/posts/index.html.erb:

...
<%= link_to 'Comments', post_comments_path(post) %>
...

Post and Comment each have their own scaffold generated form (not nested).

<%= form_for(@post) do |f| %>
...

<%= form_for(@comment) do |f| %>
...

In the comments index I loop over post comments:

views/comments/index.html.erb:

<% @post = Post.find(params[:post_id]) %>  //works fine  
<% @post.comments.each do |comment| %>
...
<% end %>

Yet after adding a new comment (under a specific post id) the table in the post comments index is empty!

Please help.
Thanks 🙂

  • 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-06-15T04:52:25+00:00Added an answer on June 15, 2026 at 4:52 am

    I figured it out.

    In the comments forms it should be:

    <%= form_for([@post, @comment]) do |f| %>
    ...
    

    Paths should be used like:

    post_comments_path(@post)
    edit_post_comment_path(@post,@comment)
    

    etc.

    In the Comments controller:

    def index
        @post= Post.find(params[:post_id])
        @comments= @post.comments.all
    ...
    
    def show
        @post= Post.find(params[:post_id])
        @comment= @post.comments.find(params[:id])
    ...
    

    etc.

    Hope others will find this useful!

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

Sidebar

Related Questions

I have found plenty of information out there about this error: 'ERROR: Could not
I've found plenty of other topics similar to this one, but I they are
found this little code snippet that seems to do what i want, but im
I've found plenty of great responsive image gallery's such as Flexslider , but the
Wondering how to open many new windows with Javascript. I have found plenty of
On my website I have plenty of form and fields, I have a system
I've found plenty of tutorials / questions on Stackoverflow that deal with copying char
I've looked everywhere, found plenty of articles, explaining work a rounds for IE 7,8
I'm sure this is asked plenty and i've found some questions on here similar
Found related questions but not the exact variant so I am posting a very

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.