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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:32:14+00:00 2026-05-22T12:32:14+00:00

I currently have a simple app that includes user authentication through devise and a

  • 0

I currently have a simple app that includes user authentication through devise and a message model(the message model uses Jquery and Faye). Both are working fine independently, but I would like to add a user_id to the messages.

I have already updated the schema and models with the relationship, but I am having trouble inputting the necessary code to have the view and controller input the relationship into the db, while keeping the jquery working. I wasn’t sure of the best way, but here I tried to create a hidden field that would pull the user_id, not sure if this is even possible. Here is the applicable code, any help is appreciated.

Message index.html.erb

<ul id="chat">
  <%= render @messages %>
</ul>

<%= form_for Message.new, :remote => true do |f| %>
<div class="field">
 <%= f.text_field :content %>
</div>
<div class="field">
 <%= f.hidden_field :user_id %>
</div>  
<div class="actions">
 <%= f.submit "Send" %>
</div>
<% end %>

create.js.erb for messages

 <% broadcast "/messages" do %>
 $("#chat").append("<%= escape_javascript render(@user.message) %>");
 <% end %>
 $("#new_message")[0].reset();

Messages Controller

class MessagesController < ApplicationController
  def index
   if @authentications = current_user
    @messages = Message.all
   else
    redirect_to authentications_url
    flash[:notice] = "You need to sign in before answering questions"
   end
  end

 def create
  @user = User.find(params[:user_id])
  @message = @user.message.create(params[:message])
 end
end

I think you have everything you would need, but if not, let me know and I will be happy to provide it for you.

Thanks, everyone.

  • 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-22T12:32:15+00:00Added an answer on May 22, 2026 at 12:32 pm

    two things to correct,

    1)use user association to create message instance in form(probably current_user if logged-in user create a message)

    <%= form_for user.messages.new, :remote => true do |f| %> #// assuming its has many association
    

    2) if it is has_many association then change association in create action

    @message = @user.messages.create(params[:message])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app, I have a User model, which includes a number of attributes
I have a simple GAE app that includes a login/logout link. This app is
I have a simple app that consists of: Model Items Filter criteria applied to
We have an existing windows desktop app written in C# 6 that uses an
I have an app that currently holds all state in memory. It fetches a
There is probably is simple fix for this but I currently have code similar
Currently, I have some basic code to play a simple tone whenever a button
I have been trying to get more in to TDD. Currently keeping it simple
I currently have an MS Access application that connects to a PostgreSQL database via
As part of my app's config process, I have a sanity checker that validates

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.