2014-04-04 21 views
1

我正在使用郵箱gem,我正在向用戶收件箱發送問題,以便他們可以查看和回答問題。我正在收到參數錯誤wrong number of arguments (0 for 3..6)。它指向行@message = current_user.send_message.new(:subject => "You have a question from #{@question.sender_id}",錯誤的參數數量(0代表3..6)

我試圖使用發送消息實例,因爲代碼@message = current_user.messages.new(:subject => "You have a question from #{@question.sender_id}"的原始行將提交問題到通知表,但不連接到會話表。

問題控制器:

def create 
    @question = Question.new(params[:question]) 
    if @question.save 
    #Original code @message = Message.create 
    @message = current_user.send_message.new(:subject => "You have a question from #{@question.sender_id}", 
          #Original code :sender_id 
          :notification_id => @question.sender_id, 
          #Original code :recipient_id 
          :receiver_id => @question.recipient_id, 
          :body => @question.question) 

    @question.message = @message 
    @question.save 
    redirect_to questions_path, notice: 'Your question was saved successfully. Thanks!' 
    else 
    render :new, alert: 'Sorry. There was a problem saving your question.' 
    end 
end 
+1

我們可以看到'send_message'方法的實現? – MrYoshiji

+0

'send_message'只在問題控制器的創建操作中。另一個位置在Messages控制器內部用於創建操作'current_user.send_message(@recipient,params [:body],params [:subject])''。 –

+1

這就是它,在你的評論你使用'send_message'方法3個參數,但在你的創建方法你使用'current_user.send_message.new'(使用send_message 0參數,而不是3..6) – MrYoshiji

回答

1

你所得到的錯誤,因爲send_message法(mailboxer寶石提供)要求3..6參數和你不發送任何參數給它。

另外,看着你的代碼,好像你正在試圖爲question保存message記錄。所以,你的@message應設置爲:

@message = current_user.message.new(:subject => "You have a question from #{@question.sender_id}", 
          #Original code :sender_id 
          :notification_id => @question.sender_id, 
          #Original code :recipient_id 
          :receiver_id => @question.recipient_id, 
          :body => @question.question) 
@question.message = @message 
@question.save 

注意我用current_user.message.new代替current_user.send_message.new

EDIT

對於使用send_message,則至少需要傳遞3個參數:

send_message(recipient, "Body", "subject") 

其中recipient是接收消息的人。

我不知道你爲什麼在send_message(因爲它通常被稱爲create a new instance of a Class)之後鏈接new方法。

你可以叫send_message如下:

@message = current_user.send_message(@question.recipient_id, @question.question, "You have a question from #{@question.sender_id}") 
+0

我必須使用'send_message'方法,否則它不會創建需要的'conversation_id'並連接到會話表。你所做的改變是我之前做過的,這就是爲什麼我改用不同的方法,因爲它沒有完成我所需要的。 –

+0

@CorneliusWilson查看我更新的答案。 –

+0

我還沒有嘗試過代碼,但看着我沒有看到如何工作,因爲我需要指定正文和主題。你只需要它的變量,而不指定它們的用途。所以我認爲它不會連接到您的更改通知表。除非我明白這一切都是錯誤的。 –

相關問題