1

我正在将 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 %>

但是我不能发送消息......(顺便说一句,我可以发送消息是控制台,并且还用“current_user.send_message(@recipient,“test”,“test”)”替换部分消息控制器,但绝对不是我想要的是)

4

1 回答 1

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"

真的希望这会有所帮助!

于 2014-01-30T05:38:36.367 回答