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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:40:44+00:00 2026-05-23T10:40:44+00:00

This is is how I have my models define for a simple blog def

  • 0

This is is how I have my models define for a simple blog

def User
  has_many :comments, :dependent => :destroy
  has_many :posts
end

def Post
  belongs_to :user
  has_many :comments
end

def Comment
  belongs_to :user
  belongs_to :post
end

In my Post Controller I have this code so that I can create a comment in the view

  def show
    @post = Post.find(params[:id])
    @comment = Comment.new
    respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @post }
    end
  end

Then in my Comment#create I have this

  def create
    @comment = current_user.comments.create(params[:comment])
    if @comment.save
      redirect_to home_show_path
    else
      render 'new'
    end
  end

How should I make it so that my comment model can receive the post_id? I have done this in my Post show view as a fix but is there another way that is better?

<%= f.hidden_field :post_id, :value => @post.id %>
  • 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-23T10:40:44+00:00Added an answer on May 23, 2026 at 10:40 am

    There’s nothing necessarily wrong with setting the post_id via a hidden field in your form – however it does mean that people could potentially associate their comment with any random post.

    A better way might be to use nested resources for the comments of posts. To do this, set the following in your routes.rb file:

    resources :posts, :shallow => true do 
      resources :comments
    end
    

    Then your form should look like this:

    <%= form_for @comment, :url => post_comments_path(@post) do %>
       ...
    <% end %>
    

    Which will mean that the form POSTs to the path /post/[:post_id]/comments – which means in turn that the post_id is available to the controller as a param:

    def create
       @comment = current_user.comments.new(params[:comment])
       @comment.post = Post.find(params[:post_id])
       if @comment.save
          ...
       end
    end
    

    This has the advantage of doing a select for the Post using the post id, and if the Post isn’t found, an error will be raised.

    It might also be worth rewriting that controller method slightly, so that the Post.find comes first:

    def create
       @post = Post.find(params[:post_id])
       @comment = @post.comments.new(params[:comment])
       @comment.user = current_user
       if @comment.save
          ...
       end
    end
    

    Hope that helps.

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

Sidebar

Related Questions

I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here
I'm trying to setup a simple has_many :through => association. I have 3 models.
I have two dependent models with one to many association class Post < ActiveRecord::Base
This blog post of the ADO.NET team shows in an example how to define
I have a very simple method: Class Team(models.Model): def sides(self): return SideNames.objects.filter(team=self) SideNames is
In this situation I have two models, Comment and Score. The relationship is defined
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,

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.