2013-10-11 21 views
1

我正在使用郵箱寶石的郵件功能,但由於我很新,我不知道如何使用它。基本上我創建了以下消息控制器:Rails郵箱寶石無法發送消息

class MessagesController < ApplicationController 
    before_action :set_message, only: [:new, :create] 

    def new 
    @message = Message.new 
    end 

    def create 
    current_user.send_message(@recipient, message_params(:body, :subject)) 
    redirect_to conversations_path 
    end 

    private 

    def set_message 
    @recipient = User.find(params[:recipient_id]) 
    end 

    def message_params 
    params.require(:message).permit(:body, :subject) 
    end 
end 

然後我的觀點:

<h1>New Message</h1> 

<%= simple_form_for(@message, html: {class: "form-horizontal"}) do |f| %> 
    <%= f.error_notification %> 

    <%= f.input :subject %> 
    <%= f.input :body, as: :text, input_html: { rows: "3"} %> 

    <div class="form-actions"> 
    <%= f.button :submit, class: "btn btn-primary" %> 
    </div> 
<% end %> 

但我無法發送消息...... (順便說一句,我可以發送消息是控制檯,並同時更換與 「current_user.send_message(@recipient, 」測試「, 」測試「)」 的消息控制器的一部分,但絕對不是我想要的)

+0

另外,如果您發佈準確的堆棧錯誤,您將得到... – David

+1

你有沒有想過這個答案?我試圖做到現在,我得到的錯誤,我有一個未定義的模型名稱。我不明白,因爲寶石不應該照顧那個?我們是否需要製作消息模型?如果是這樣,那麼桌子上需要放什麼? – Philip7899

回答

1

使用form_tag的替代的form_for

而不是使用的form_for的,請嘗試使用的form_tag,這已經爲我工作:

<%= form_tag("/messages/send_message", method: "post", url: send_message_path) do %> 
<%= hidden_field_tag :user_id, "#{@user.id}" %> 
<%= text_field_tag :subject, nil, :class => 'form-control', :placeholder => "Subject" %> 
<%= text_area_tag :message, nil, :class => 'form-control', :placeholder => "Message" %> 
<%= submit_tag "Submit", :class => "btn btn-primary" %> 
<% end %> 

這將有一個動作,你的消息控制器是這樣的:

def send_message 
@user = User.find(params[:user_id]) 
@message = params[:message] 
@subject = params[:subject] 
current_user.send_message(@user, "#{@message}", "#{@subject}") 
redirect_to root_path 
end 

而且這樣的事情在你路線文件:

post '/messages/send_message', :to => "messages#send_message", :as => "send_message" 

真的希望這有助於!