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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:09:18+00:00 2026-06-02T07:09:18+00:00

I have a message model and a user model. my message belongs_to my user

  • 0

I have a message model and a user model. my message belongs_to my user and user has_many messages.

I’m trying to allow a user to private message another user while on their public profile page (their show template). I have tried a number of attempts, but I ultimately run back into the issue of requiring an ID to be attr_accessible (which I heard is bad to do). Am I doing something wrong?

My message model, I have :user_id (which is the current user, aka a sending_from ID), :to_id, :content.

When I’m looking at a users profile page, on the show template I have

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

In my user 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], to_id: @user.id)
   end
end

when the form submits, it goes to my message create action

def create
    @message = current_user.messages.build(params[:message])
    redirect_to user_path(params[:message][:to_id])
end

However, I always get the error

`Can't mass-assign protected attributes: to_id`

It seems like I can fix it by making :to_id attr_accessible, however I have heard it is not very safe to do so. Am I doing something wrong? This issue has been killing me.

Any help would be appreciated. Thanks

  • 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-02T07:09:19+00:00Added an answer on June 2, 2026 at 7:09 am

    Making to_id accessible is fine. But if you don’t want that error just fix it like this:

    def create
        @message = current_user.messages.build
        @message.to_id = params[:message][:to_id]
        # manually assign whatever other params you need to
        redirect_to user_path(params[:message][:to_id])
    end
    

    Mass assignment just means you can’t use update_attributes, you can still use model.attribute=. The reason for doing it that way might be to add additional whitelisting parameters, such as:

    def create
      safe_params = params[:model].slice(:safe_attr1,:safe_attr2)
      @model = Model.new(safe_params)
      whitelist = ['some_safe_string','another_safe_string']
      if whitelist.include?(params[:model][:dangerous])
        @model.dangerous_attribute = params[:model][:dangerous]
      end
      @model.save
      redirect_to @model
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a user model and a message model. my user has_many messages and
I have a model User which has_many :messages and Message which belong_to :user. when
I have a User and Group model. User has_many Groups and Group belongs_to User
message and user. my message belongs_to user and user has_many messages. in one of
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have this Pin Model: class Pin < ActiveRecord::Base belongs_to :user belongs_to :image accepts_nested_attributes_for
To start, I have 3 Models: Location has_many :products User has_many :products Products belongs_to
I have models User and Message. Every user can send messages to other users.
I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user

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.