使用Rails gem'Mailboxer'和sender_id始终为零 [英] Using the Rails gem 'Mailboxer' and the sender_id is always zero

查看:130
本文介绍了使用Rails gem'Mailboxer'和sender_id始终为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我使用friendly_id gem作为用户名,我认为这是问题所在,但我无法找出如何解决它(如果它甚至是问题)。

messages / new.html.erb

 <%provide (:标题,新消息)%> 

<%= form_tag user_messages_path,方法::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'收件人','选择收件人'%>
<%= select_tag'recipients',recipients_options(@chosen_recipient),multiple:true,class:'form-control chosen-it'%>
< / div>

<%= submit_tag'发送',类:'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'])
对话= 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
$ b private b
$ b def authenticate_user
除非((current_user.id = params [: user_id])除非current_user.nil?)
flash [:error] ='看起来你不应该在那里'
redirect_to login_path
end
end
end

routes.rb

 资源:用户做
资源:会话,只有:[:index,:show,:destroy] do
成员做
发帖:回复
发帖:恢复
删除:empty_trash
发布:mark_as_read
结束
结束
资源:消息,仅:[:new,:create]
结束

我主要关注本教程在集成gem时。我不知道为什么它们在发送新消息时没有正确保存用户标识。



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

解决方案

我不太确定是否遵循 authenticate_user 实现:

  def authenticate_user 
除非((current_user.id = params [:user_id])除非current_user.nil?)
flash [:error] ='看起来你不应该是有'
redirect_to login_path
结束
结束

您可能需要以简化它。



如果您试图比较 current_user.id params [ :user_id] ,您需要使用 current_user.id == params [:user_id] ,而不是 current_user.id = PARAMS [:USER_ID] 。它的 == ,而不是 =


Just integrated Mailboxer and I'm running into one problem. When I send a new message the sender_id in the mailboxer_notifications table is always zero instead of the id of the sender.

I'm using the friendly_id gem for usernames and I think this is where the problem is but I cant find out how to fix it (if it even is the problem).

messages/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

I mostly followed this tutorial when integrating the gem. I have no idea why it isn't saving the user id properly when they send a new message.

update The reason I suspect friendly_id is causing it so save the sender_id as 0 is because when I don't use friendly id in the url e.g. users/1/messages/new it works fine but users/example-user/messages/new it doesn't save the user id when creating a new message

解决方案

I am not quite sure if I follow your authenticate_user implementation:

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

You may want to simplify it.

If you are trying to compare current_user.id to params[:user_id], you need to use current_user.id == params[:user_id], not current_user.id = params[:user_id]. Its ==, not =.

这篇关于使用Rails gem'Mailboxer'和sender_id始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆