0

I'm using the mailboxer gem to implement user messaging on an app. Currently, i've got it set up based on this https://github.com/RKushnir/mailboxer-app. Users can enter an email address, subject and message into a form, and start a conversation.

I'd like a user to be able to start a conversation without having to enter another users email address - for instance clicking a button on a relevant page where I can pass a @user in as the recipient.

Here is my current controller:

class ConversationsController < ApplicationController
      authorize_resource
      helper_method :mailbox, :conversation


      def create
        recipient_emails = conversation_params(:recipients).split(',')
        recipients = User.where(email: recipient_emails).all

        conversation = current_user.
          send_message(recipients, *conversation_params(:body, :subject)).conversation

        redirect_to conversation
      end

      def reply
        current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
        redirect_to conversation
      end

      def trash
        conversation.move_to_trash(current_user)
        redirect_to :conversations
      end

      def untrash
        conversation.untrash(current_user)
        redirect_to :conversations
      end

      private

      def mailbox
        @mailbox ||= current_user.mailbox
      end

      def conversation
        @conversation ||= mailbox.conversations.find(params[:id])
      end

      def conversation_params(*keys)
        fetch_params(:conversation, *keys)
      end

      def message_params(*keys)
        fetch_params(:message, *keys)
      end

      def fetch_params(key, *subkeys)
        params[key].instance_eval do
          case subkeys.size
          when 0 then self
          when 1 then self[subkeys.first]
          else subkeys.map{|k| self[k] }
          end
        end
      end
    end

and corresponding _form:

<%= form_for :conversation, url: :conversations do |f| %>
      <%= f.text_field :recipients %>
      <%= f.text_field :subject %>
      <%= f.text_field :body %>
      <div class="form-actions">
        <%= f.button :submit, class: 'btn-primary' %>
        <%= submit_tag 'Cancel', type: :reset, class: 'btn btn-danger' %>
      </div>
    <% end %>

I've got all the views and actions working relating to replying to conversations with messages, display all users conversations etc, but just not how to start it without having to type the address in.

Thanks for any help

Added: I don't mind editing the above form etc and losing the capability for a user to enter another's email, as I don't want them to be able to do that. The buttons in question will be named along the lines of 'ask the client a question', or 'ask the venue a question', rather than just sending messages to random users.

EDITED TO ADD:

Thanks for that, makes sense and I am nearly there...

I've ended up adding a copy of the original conversation form i'm using (in my _form file):

 <%= form_for :conversation, url: :conversations do |f| %>
        <%= f.text_field :recipients %>
        <%= f.text_field :subject %>
        <%= f.text_field :body %>
        <div class="form-actions">
          <%= f.button :submit, class: 'btn-primary' %>
          <%= submit_tag 'Cancel', type: :reset, class: 'btn btn-danger' %>
        </div>
      <% end %>

This works fine from the correct view that I need it in, and does everything as expected - however, when I try to hide the recipients text field and pass in the users email address (rather than having to manually type it in, I am getting problems:

 <%= form_for :conversation, url: :conversations do |f| %>
        <%= hidden_field_tag :recipients, "#{@enquiry.client.user.email}" %>
        <%= f.text_field :subject %>
        <%= f.text_field :body %>
        <div class="form-actions">
          <%= f.button :submit, class: 'btn-primary' %>
          <%= submit_tag 'Cancel', type: :reset, class: 'btn btn-danger' %>
        </div>
      <% end %>

This is throwing an error in the controller:

undefined method `split' for nil:NilClass

 def create
recipient_emails = conversation_params(:recipients).split(',')
recipients = User.where(email: recipient_emails).all
conversation = current_user. 

If I 'view source' on Firefox before I hit submit on the form, I can see that there is a field there with the users email address passed in correctly, but it's just not going to the controller for some reason?

Something that may help - If I change the hidden_field_tag back to a normal field, and pass in the email address as above, the view won't even load. It is saying 'undefined method `merge' for "user@user.com":String and highlighting that row in the code.

Thanks

4

1 回答 1

1

在邮箱中,您不会将消息发送到电子邮件地址,而是将其发送到其上的模型acts_as_messageable,例如用户。以下代码是我如何完成它的粗略实现,@user变量在控制器中设置,因为这是在配置文件页面上。

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

或者,在您的情况下,如果您想要电子邮件地址,您可以通过数据库中的电子邮件地址找到收件人:

def send_message
@user = User.find_by_email(params[:user_email])
@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-02-05T04:54:22.127 回答