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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:58:58+00:00 2026-06-15T09:58:58+00:00

I read this post and it was helpful for the first step, now I’m

  • 0

I read this post and it was helpful for the first step, now I’m at the second step. I need help with the controllers and forms.

Let’s say this is my model:

User
 has_many :posts
 has_many :comments

Post
 belongs_to :user
 has_many :comments

Comment
 belongs_to :user
 belongs_to :post

And these are my tables:

User
 id

Post
 id
 user_id

Comment
 id
 user_id
 post_id

In the initial post, the User ID gets added with the content automatically by adding this to the create section in the controller:

@post = current_user.post.build(params[:post])

If the comment belongs to both the user and the post, how do I make it automatically add the post_id as well as the user_id? Currently, I can only seem to make it insert the user_id by doing this:

current_user.comments.build(params[:comment])

I’m new to rails. I don’t think the best way would be to make the field accessible and add a hidden field in the form, isn’t there another way?

These are my updated routes:

                  users GET    /users(.:format)                                           users#index
                        POST   /users(.:format)                                           users#create
               new_user GET    /users/new(.:format)                                       users#new
              edit_user GET    /users/:id/edit(.:format)                                  users#edit
                   user GET    /users/:id(.:format)                                       users#show
                        PUT    /users/:id(.:format)                                       users#update
                        DELETE /users/:id(.:format)                                       users#destroy
               sessions POST   /sessions(.:format)                                        sessions#create
            new_session GET    /sessions/new(.:format)                                    sessions#new
                session DELETE /sessions/:id(.:format)                                    sessions#destroy
   Post_comments GET    /posts/:Post_id/comments(.:format)          comments#index
                        POST   /posts/:Post_id/comments(.:format)          comments#create
 new_Post_Comment GET    /posts/:Post_id/comments/new(.:format)      comments#new
edit_Post_Comment GET    /posts/:Post_id/comments/:id/edit(.:format) comments#edit
     Post_Comment GET    /posts/:Post_id/comments/:id(.:format)      comments#show
                        PUT    /posts/:Post_id/comments/:id(.:format)      comments#update
                        DELETE /posts/:Post_id/comments/:id(.:format)      comments#destroy
          posts GET    /posts(.:format)                                   posts#index
                        POST   /posts(.:format)                                   posts#create
       new_Post GET    /posts/new(.:format)                               posts#new
      edit_Post GET    /posts/:id/edit(.:format)                          posts#edit
           Post GET    /posts/:id(.:format)                               posts#show
                        PUT    /posts/:id(.:format)                               posts#update
                        DELETE /posts/:id(.:format)                               posts#destroy
                   root        /                                                          static_pages#home
                 signup        /signup(.:format)                                          users#new
                 signin        /signin(.:format)                                          sessions#new
                signout DELETE /signout(.:format)                                         sessions#destroy
                  start        /start(.:format)                                           posts#new
  • 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-15T09:58:59+00:00Added an answer on June 15, 2026 at 9:58 am

    The best way that I’ve found to do commenting has been covered by Ryan Bates on railscasts here

    class Comment < ActiveRecord::Base
      attr_accessible :content
      belongs_to :commentable, polymorphic: true
    end
    
    class Post < ActiveRecord::Base
      attr_accessible :content, :name
      has_many :comments, as: :commentable
    end
    

    And you can pass current_user when actually building the comments in the controller, or pass it as a hidden field, a few different options available to you.

    Edit for routes:
    Ah that’s why, you’re comments and your posts aren’t hooked into each other at all. in your routes where you do resources :post change it do

    resources :post do 
      resources :comments 
    end
    

    You need to associate the comments to the posts if you’re doing Post.comments.build
    If you’re doing current_user.comments.build you’ll need to do this in your routes

    resources :user do
      resources :comments
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read this helpful post : SharedPreferences.onSharedPreferenceChangeListener not being called consistently However I
I haver read this post to: Apple Push Notifications to specific Users I need
I've read this post which is really helpful. However I'm trying to open attachments
I read this very helpful post: how to render html to a string instead
I read this post where the author advices to store session files in a
I read this post but I can't get it working: Change Background Color... I
Okay so I read this post http://facebook.stackoverflow.com/questions/10373897/deleting-a-previosly-posted-article-with-opengraph-or-check-if-said-article-has but I'm using the news.reads action type.
I was reading about buffer, stack and heap overflows. I read this post as
On this post , I read about the usage of XMPP. Is this sort
I read this comment in the OpenID post on the stackoverflow blog. Kibbee says

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.