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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:07:12+00:00 2026-06-02T13:07:12+00:00

im trying to create a simple private messaging system i have a nested route

  • 0

im trying to create a simple private messaging system

i have a nested route

resources :users do
   resources :messages
end

on my users show page, i have…

    <%= form_for([current_user, @message])  do |f| %>
      <div class="field">
        <%= f.text_area :content, placeholder: "Send a private message..." %>
      </div>
      <%= f.submit "Post", class: "btn btn-large btn-primary" %>
    <% end %>

where the current user can send a private message to the user currently being looked at.

on my message.rb i have…

belongs_to :user

and in user.rb i have…

has_many :messages

on my user controllers show action i have

def show
 @user = User.find(params[:id])
 @microposts = @user.microposts.paginate(page: params[:page])
 if user_signed_in?
    @message = current_user.messages.build(params[:messages])
 end

end

when a user hits the submit button on my show template, it goes to my messages create action, however im unable to get the id of the user i was looking at (or trying to send to)

i printed out my message variable, and it says…

[#<Message id: nil, content: nil, user_id: 1, to_id: nil, created_at: nil, updated_at: nil>]

the :user_id is the current user (which is me, with an id of 1), however i can’t seem to get the to_id (which was the user i was just looking at, lets say his id was 2)

can someone please tell me how i can retrieve it? i can only think of passing the id as a hidden field, but i heard that’s not safe to do.

help would be greatly appreciated. thanks

update: @Andrew

hmm actually its interesting because if i do…

@message = current_user.messages.build(to_id: @user.id)

and i pass in

<%= f.hidden_field :to_id, :value => @user.id %>

i get a

Can't mass-assign protected attributes: to_id. 

does anyone know why?

  • 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-02T13:07:17+00:00Added an answer on June 2, 2026 at 1:07 pm

    There’s nothing wrong with putting a UserID in a hidden field, it’s visible in the URL to begin with.

    If you add the to_id in your show action and have a hidden field on that in your form it will flow from the users#show action into the message form and then to the messages#create action when that form is posted.

    def show
     @user = User.find(params[:id])
     @microposts = @user.microposts.paginate(page: params[:page])
     if user_signed_in?
        @message = current_user.messages.build(to_id: @user.id)
     end
    
    end
    

    Note that the show action is not receiving any params[:messages], because you do not post to this. Unless your URL looks like this: users/1?messages=foo then that params value will always be nil.

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

Sidebar

Related Questions

I am trying to create a simple page that enters data in to a
I'm trying to create a simple web-page, which is displaying the data, received from
I am trying to create a simple console based java application, which requires users
I have been trying to create a simple app that parse an entire channel
I have been trying to create a simple class, which defines default property values
I am trying to create a very simple help page in my app. The
As part of learning ruby/rails, I'm trying to implement http://github.com/professionalnerd/simple-private-messages into my application from
I'm studying webrat and cucumber and trying to create simple example. Here is my
Im trying to create a simple pan and zoom app using silverlight 4, but
Im trying to create a simple input box with form validation, but im not

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.