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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:25:48+00:00 2026-05-27T15:25:48+00:00

So Im working on a rails app where users can comment on photos or

  • 0

So Im working on a rails app where users can comment on photos or videos another user has uploaded and so far everything is great except I am not able to get the current user_id associated with the person who has commented on the post. This is what I have so far.

user.rb

has_many :comments,  :dependent => :destroy

photo.rb

has_many :comments, :as => :commentable

video.rb

has_many :comments, :as => :commentable

comments_controller.rb

def create
  @commentable = find_commentable
  @comment = @commentable.comments.build(params[:comment])
 if @comment.save
   redirect_to :id => nil, :notice => "Successfully created comment."
 else
   render :action => 'new'
 end
end

How can I get the user id to appear with the current comments? I have the comment type and comment id I am just looking for a way to have it so the user_id can appear. Any suggestions?

  • 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-27T15:25:49+00:00Added an answer on May 27, 2026 at 3:25 pm

    You should add a hidden_field to your form partial where you store the current_user.id

    something like:

      <%= f.hidden_field :user_id, :value => current_user.id %> 
    

    of course you should have a field user_id in your comment model, as a comment belongs_to user and a user has_many comments.

    update:

    what ofca pointed out, this can approach can lead to security issues as the hidden field could be modified by the user in the browser, e.g. using firebug.
    In this case it is probably better to to leave out this field in the view and create the comment in the controller by using

    <%= current_user.comments.create(params[:comment]) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Rails app that existing users can invite additional members to
I'm working on an app where users can share photos. The photos can optionally
Im working on a rails app where a user has the ability to upload
Im working on an Occasion Reminder rails app where users can register then set
I am working on a rails app that allows users to log in via
I'm working on creating a Rails app that allows users to set availability Sunday
I did heroku create and pushed a working rails app to the server. Everything
I'm working on a rails app that has a whole bunch of before filters
I'm working on a rails app that integrates with other systems (which can't be
I'm building a Rails 3 app where users can select one of a number

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.