【问题标题】:Using the Rails gem 'Mailboxer' and the sender_id is always zero使用 Rails gem 'Mailboxer' 并且 sender_id 始终为零
【发布时间】:2016-03-20 02:14:58
【问题描述】:

刚刚集成 Mailboxer,我遇到了一个问题。当我发送一条新消息时,mailboxer_notifications 表中的sender_id 始终为零,而不是发件人的 id。

我正在为用户名使用friendly_id gem,我认为这是问题所在,但我不知道如何解决它(如果它甚至是问题)。

消息/new.html.erb

<% provide(:title, "New Message") %>

<%= form_tag user_messages_path, method: :post do %>

  <div class="form-group">
    <%= label_tag 'message[subject]', 'Subject' %>
    <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= label_tag 'message[body]', 'Message' %>
    <%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
  </div>


<div class="form-group">
  <%= label_tag 'recipients', 'Choose recipients' %>
  <%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
</div>

  <%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>

messages_controller.rb

class MessagesController < ApplicationController
  before_action :authenticate_user

  def new
    @user = current_user
    @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
  end

  def create
    @user = current_user
    recipients = User.where(id: params['recipients'])
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
    flash[:success] = "Message has been sent!"
    redirect_to user_conversation_path(@user, conversation)
  end

  private

  def authenticate_user
    unless ((current_user.id = params[:user_id]) unless current_user.nil?)
      flash[:error] = 'Looks like your not supposed to be there'
      redirect_to login_path
    end
  end
end

routes.rb

resources :users do
  resources :conversations, only: [:index, :show, :destroy] do
    member do
      post :reply
      post :restore
      delete :empty_trash
      post :mark_as_read
    end
  end
  resources :messages, only: [:new, :create]
end

在集成 gem 时,我主要关注 this tutorial。我不知道为什么当他们发送新消息时它没有正确保存用户 ID。

更新 我怀疑friendly_id 导致它的原因是将sender_id 保存为0 是因为当我不在url 中使用友好ID 时,例如users/1/messages/new 它工作正常,但 users/example-user/messages/new 它在创建新消息时不保存用户 ID

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 rubygems mailboxer


    【解决方案1】:

    我不太确定我是否遵循您的 authenticate_user 实施:

    def authenticate_user
      unless ((current_user.id = params[:user_id]) unless current_user.nil?)
        flash[:error] = 'Looks like your not supposed to be there'
        redirect_to login_path
      end
    end
    

    你可能想要简化它。

    如果您想比较 current_user.idparams[:user_id],您需要使用 current_user.id == params[:user_id],而不是 current_user.id = params[:user_id]。它是==,而不是=

    【讨论】:

    • 感谢添加 == 帮助但我现在在发送消息时收到错误 Couldn't find Mailboxer::Conversation with 'id'=2 [WHERE "mailboxer_notifications"."type" = 'Mailboxer::Message' AND "mailboxer_receipts"."receiver_id" = ? AND "mailboxer_receipts"."receiver_type" = ?]
    猜你喜欢
    • 2014-10-26
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多