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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:07:47+00:00 2026-05-15T07:07:47+00:00

I have many instances of a Rails model, Post . When viewing an individual

  • 0

I have many instances of a Rails model, Post. When viewing an individual post, I’d like to create a form to create a child of Post called Comment. I’d like to prepopulate this form with a hidden tag that contains the post_id which is the foreign key in Comment.

The Railsy way to do this is to create a fancy-ish route such as:

/comments/new/post/:post_id

However, this gunks up the routes file and doesn’t leave much flexibility. Let’s say I want to create a link somewhere else that prepopulates a different attribute of the form…then I’d have to add another route for this.

So I think I’m going to create urls like this on /posts/show/:id:

/comments/new?comment[post_id]=<%= @post.id %>

This way I can add any other attributes as I need. I know the plus side associated with this, now what are the downsides?

  • 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-15T07:07:48+00:00Added an answer on May 15, 2026 at 7:07 am

    Just use new_comment_path :comment => { :post_id => @post.id } to create such URLs. You could wrap it into a helper if you’d like.

    However, there should be no downside with the /comments/new/post/:post_id URL style, as you can add further parameters, too:

    new_post_comment_path @post, :comment => { :additional => "parameters", ... }
    

    would result in

    /posts/:post_id/comments/new?comment[additional]=parameters&...
    

    and in your action do:

    def new
      @post = Post.find params[:post_id]
      @comment = @post.build params[:comment]
    end
    

    and render your form…

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

Sidebar

Related Questions

First of all I have many Django instances setup and running like this. In
I have a 'Cost' model in rails. Something like the following: class Cost <
I am using Ruby v1.9.2 and Ruby on Rails v3.2.2. I have many model
I have a class that I can have many instances of. Inside it creates
I have only one class with many instances. Every instance is observer of couple
I have a large codebase which has many, many instances of the try {
We have an ASP.NET web application hosted by a web farm of many instances
I have a Class, it will be implemented to be many instances. I want
I have similar instances of the same web application running across many domains. I
I have a complex form for a model Schedule that belongs to a parent

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.