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

  • SEARCH
  • Home
  • 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 8947917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:53:10+00:00 2026-06-15T12:53:10+00:00

I have a model Post that has_many :comments . The form that will post

  • 0

I have a model Post that has_many :comments. The form that will post the comment will be shown along with the post in posts/show.html.erb. I have a comments_controller that should handle the creation of comments. Searching on google, I found

<%= form_for([@post, Comment.new], :controller => 'comments', :action => 'create') do |f| %>

But this doesn’t work. How do I do this ?

  • 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-15T12:53:10+00:00Added an answer on June 15, 2026 at 12:53 pm
    class Post < ActiveRecord::Base
      has_many :comments
    
      accepts_nested_attributes_for :comments
    #...
    
    class Comment < ActiveRecord::Base
      belongs_to :post
    #...
    

    Then in the form

    form_for @post do |f|
      f.fields_for :comments do |c|
        c.text_field :title
        #...
      f.submit
    

    this would create the associated object through active record’s accepts_nested_attributes_for, which doesn’t require a separate comments_controller. you are submitting to the posts controller, which is handling creating the associated object during the update of the post.

    with a comments_controller, you could do one of two things:

    send item_id as a param to comments_controller#new, grab the item, then build the new comment from it

    @post = Post.find(params[:item_id); @comment = @post.comments.build
    

    put the post_id in a hidden field on the form and just create the comment as normal

    # in the controller
    @comment = Comment.create(params[:comment])
    
    # in the view
    form_for @comment do |f|
      f.text_field :title
      #...
      f.hidden_field :post_id, value: @post.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comment form (in comments/_form.html.erb) that I use in my other controllers
I have a Post model that has_many :comments . The question is: how can
I have a post controller that has many comments. The post model has a
For example I have in my app model User, that has_many models Post. And
Suppose I have a Post model which has_many Comments. I want to get the
So I have a simple post model that has_many tags :through post_tags. This allows
Let's say I have a User model which has_many :posts , while Post has_many
I have a comments model that belongs to two models: submissions and posts class
I have a post model that has many tags through taggings. let's say: @posts
I have a Topic model and a Post model. A Topic has many Posts,

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.