我一直在Rails應用程序中構建消息,以便用戶能夠發送其他消息。我已經看過郵箱等一些寶石,但最終決定自己創建。Rails中用戶到用戶的消息
我希望有人能幫助我把這些碎片放在一起。我一直在回答類似問題的答案here。
我在軌測試控制檯和我不斷收到以下錯誤:
未定義的方法`send_message」爲#
我怎樣才能解決這個問題?
控制器
class MessagesController < 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
路線
resources :users, :except => [ :create, :new ] do
resources :store
resources :messages, :only => [:create, :destroy]
end
消息型號
class Message < ActiveRecord::Base
belongs_to :user
scope :sent, where(:sent => true)
scope :received, where(:sent => 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 => from.id, :sent => true
end
end
send_message不是一個類方法,消息需要實例化才能使用'send_message'。試試這個'def self.send_message(from,recipients)' – MrYoshiji