【问题标题】:Rails Mailboxer - Create Conversation from ButtonRails Mailboxer - 从按钮创建对话
【发布时间】:2014-02-28 17:26:48
【问题描述】:

我正在使用mailboxer gem 在应用程序上实现用户消息传递。目前,我已经根据https://github.com/RKushnir/mailboxer-app 进行了设置。用户可以在表单中输入电子邮件地址、主题和消息,然后开始对话。

我希望用户能够开始对话而无需输入其他用户的电子邮件地址 - 例如,单击相关页面上的按钮,我可以将@user 作为收件人传递。

这是我当前的控制器:

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

以及对应的_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 %>

我已经掌握了与使用消息回复对话、显示所有用户对话等相关的所有视图和操作,但不知道如何在无需输入地址的情况下启动它。

感谢您的帮助

补充:我不介意编辑上述表格等并失去用户输入他人电子邮件的能力,因为我不希望他们能够这样做。有问题的按钮将按照“向客户提问”或“向场地提问”的方式命名,而不仅仅是向随机用户发送消息。

编辑添加:

谢谢你,有道理,我快到了......

我最终添加了我正在使用的原始对话表单的副本(在我的 _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 %>

从我需要它的正确视图来看,这可以正常工作,并且按预期执行所有操作 - 但是,当我尝试隐藏收件人文本字段并传入用户电子邮件地址时(而不是必须手动输入,我遇到了问题:

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

这是在控制器中引发错误:

nil:NilClass 的未定义方法 `split'

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

如果我在点击表单提交之前在 Firefox 上“查看源代码”,我可以看到那里有一个字段,其中包含正确传递的用户电子邮件地址,但由于某种原因它没有进入控制器?

可能有帮助的东西 - 如果我将 hidden_​​field_tag 改回普通字段,并按上述方式传递电子邮件地址,则视图甚至不会加载。它在说 “user@user.com”的'未定义方法`合并':字符串 并在代码中突出显示该行。

谢谢

【问题讨论】:

    标签: ruby-on-rails mailboxer


    【解决方案1】:

    在mailboxer中,您不会将消息发送到电子邮件地址,而是将其发送到带有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"
    

    真的希望这会有所帮助!

    【讨论】:

    • 谢谢,很有帮助。我快到了,并在上面添加了另一个最终查询!努力让最后一部分工作。
    • 实际上,我应该更仔细地看一下,在这里回答:stackoverflow.com/questions/6636875/…。我会将您的答案标记为正确,因为它让我走上了正确的道路,我相信将来会帮助其他人。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    相关资源
    最近更新 更多