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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:42:22+00:00 2026-06-03T05:42:22+00:00

i have this form_for sending a private message between two users on my users#show

  • 0

i have this form_for sending a private message between two users on my users#show template

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

and in my message model, i have

belongs_to  :to,
    :class_name => 'User',
    :foreign_key => :to_id

validates :content, presence: true, length: { maximum: 1000 }
validates :user_id, :to_id, presence: true

what is weird is that the very first time a user gets created and they navigate to their show profile,

they already get this error

The form contains 2 errors.
* Content can't be blank
* To can't be blank

once i refresh the page, they disappear. and the form functions normally. i can’t seem to figure out what is wrong. does the form already try to auto submit by itself?

on my messages#create action i have

def create
    @message = current_user.messages.build
    @message.to_id = params[:message][:to_id]
    @message.user_id = current_user.id
    @message.content = params[:message][:content]
    if @message.save
        flash[:success ] = "Private Message Sent"
    end
        redirect_to user_path(params[:message][:to_id])
end

if that sheds any light. im manually assigning the @msessage attributes because i didn’t want to make :user_id attr_accessible.

but still, why does the validation already fail just by going to the page the very first time? im not submitting. once i leave the page and come back to it, the error doesn’t occur anymore.

here is my users#show action

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
    @current_user = current_user
    if user_signed_in?
        @message = current_user.messages.build(params[:messages], to_id: @user.id)
    end
    @influence = current_user.points
    @badge_items = current_user.badges.to_a.map(&:name)
  end

does it have to do something with my show action? specifically

@message = current_user.messages.build(params[:messages], to_id: @user.id)

is that calling a create?

help would be appreciated. thank you!

UPDATE. i fixed it!

instead of…

@message = current_user.messages.build(params[:messages], to_id: @user.id)

i just did

@message = current_user.messages.build

i guess the issue was more of…it was looking for the messages value and the user id, which there isnt any yet passed. thank you!

  • 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-03T05:42:23+00:00Added an answer on June 3, 2026 at 5:42 am

    Yes,

    @message = current_user.messages.build(params[:messages], to_id: @user.id)
    

    is attempting to create a Message object.

    Here’s a link to the Rails guide on has_many that describes the specifics of how messages.build will be building a new record.

    http://guides.rubyonrails.org/association_basics.html#has_many-association-reference

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

Sidebar

Related Questions

I have this in one of my haml views: -form_for :news_item_comment, :url=>{:id => comment.id,
I have a login form for my website. This login form have two text
I have a function for sending email. This function works normally, but it always
so far i have used this code.It is working when i am sending the
I have this code which works with no errors when sending "simple text" through
So i have this function in JS, sending a request to insert a new
I have this form for inputting the birthday in html. But I only have
I have this this View for rendering form @using ExpertApplication.ViewModels @model IEnumerable<QuestionViewModel> @{ ViewBag.Title
i have made a simple php contact form following this tutorial: http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme The big
In my views.py, i have a snippit of code like this: def clean_post_data(form): for

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.