Rails mailboxer gem无法发送消息

时间:2013-10-11 16:08:55

标签: ruby-on-rails mailboxer

我正在使用Mailboxer gem进行消息传递功能,但由于我很新,我不太清楚如何使用它。基本上我创建了以下消息控制器:

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 %>

但我无法发送消息...... (BTW我可以发送消息是控制台,也用“current_user.send_message(@recipient,”test“,”test“)替换部分消息控制器”,但绝对不是我想要的)

1 个答案:

答案 0 :(得分: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"

真的希望这有帮助!