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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:13:17+00:00 2026-05-18T04:13:17+00:00

Assuming the Post – Comment model with the nested resources: resources :posts do resources

  • 0

Assuming the Post – Comment model with the nested resources:

resources :posts do
  resources :comments
end

How should the app/views/comments/_form.html.haml (erb will do as well) look like so that it also provides id of the post to attach the comment to?

Current only one way I know is to manually add hidden input with the post id. It looks dirty to me.

Is there any better way? I expected rails to understand the nested resource and automatically include the post_id as a hidden input.

= form_for [@post, @comment] do |f|
  .field
    f.label :body
    f.text_field :body
    hidden_field_tag :post_id, @post.id
  .actions
    = f.submit 'Save'

EDIT: Using Mongoid, not ActiveRecord.

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-05-18T04:13:18+00:00Added an answer on May 18, 2026 at 4:13 am

    The ID of the post will actually be in the URL. If you type rake routes into your terminal/console, you will see that the pattern for your nested resource is defined as such:

    POST    /posts/:post_id/comments    {:controller=>"comments", :action=>"create"}
    

    Take a look at the HTML that is spit out by the form_for method, and look specifically at the action url of the <form> tag. You should see something like action="/posts/4/comments".

    Assuming that you’ve defined resources :comments only once in your routes.rb, as a nested resource of resources :posts, then it is safe for you to modify the CommentsController#create action as such:

    # retrieve the post for this comment
    post = Post.find(params[:post_id])
    comment = Comment.new(params[:comment])
    comment.post = post
    

    Or you can simply pass params[:post_id] to comment instance like so:

    comment.post_id = params[:post_id]
    

    I hope this helps.

    For more information on nested forms/models, I recommend watching the following Railscasts:

    • Complex Forms
    • Nested Forms
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a comments model and a posts model, What code can I
Assuming of course we're working with HTML Forms and GET/POST data, backend language agnostic.
Is it possible to post the whole html page to the server(assuming that the
Please help me with a sanity check. Assuming a many-to-many relationship: Post, PostTagAssoc, Tag
I'm using Asp.net., Assuming I'm allowing user to post messages in my site with
Assuming a setup like this: blogposts { title:Example, slug:example-post tags: [foo, bar] }, {
I'm trying to implement Redirect After Post for the first time in ASP.NET. Assuming
Assuming that you already have created an oauth client app in twitter, you can
As a follow up to this post: https://stackoverflow.com/questions/521432/best-jquery-rounded-corners-script Assuming jQuery is already being included,
I have a model named Post (blog post) and a model named Category .

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.