Note that there are some explanatory texts on larger screens.

plurals
  1. POUser to User Messages in Rails
    text
    copied!<p>I've been building messaging in a rails app for users to be able to send each other messages. I've looked at a few gems such as mailboxer but ultimately decided to build my own.</p> <p>I'm hoping someone can help me put these pieces together. I've been following a similar question's answer <a href="https://stackoverflow.com/questions/5141564/model-users-message-in-rails-3">here</a>.</p> <p>I'm testing in the rails console and I keep getting the following error: </p> <p>undefined method `send_message' for #</p> <p>How can I fix this?</p> <p><strong>Controller</strong></p> <pre><code>class MessagesController &lt; ApplicationController # create a comment and bind it to an article and a user def create @user = User.find(params[:id]) @sender = current_user @message = Message.send_message(@sender, @user) flash[:success] = "Message Sent." flash[:failure] = "There was an error saving your comment (empty comment or comment way to long)" end end </code></pre> <p><strong>Routes</strong></p> <pre><code> resources :users, :except =&gt; [ :create, :new ] do resources :store resources :messages, :only =&gt; [:create, :destroy] end </code></pre> <p><strong>Messages Model</strong></p> <pre><code>class Message &lt; ActiveRecord::Base belongs_to :user scope :sent, where(:sent =&gt; true) scope :received, where(:sent =&gt; false) def send_message(from, recipients) recipients.each do |recipient| msg = self.clone msg.sent = false msg.user_id = recipient msg.save end self.update_attributes :user_id =&gt; from.id, :sent =&gt; true end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload