メールボックスが統合されているため、1つの問題が発生しています。新しいメッセージを送信すると、のmailboxer_notifications
テーブルには、送信者のIDではなく常に0が表示されます。Railsのgem 'Mailboxer'を使用し、sender_idは常にゼロです
私はフレンドリーなid宝石をユーザー名に使用していますが、これは問題の場所だと思いますが、それを修正する方法がわかりません。
メッセージ/ 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
宝石を統合するとき、私はほとんどthis tutorialを追いました。新しいメッセージを送信するときに、なぜユーザーIDを正しく保存していないのかわかりません。
更新 理由私はfriendly_idが、それはので、私は、例えば、URLにやさしいIDを使用していないときからですsender_id
0として保存する原因になっている疑いがあります新しいメッセージを作成するときにユーザーIDを保存しません。
'== 'を助けていただきありがとうございましたが、' id '= 2のメールボックス::会話が見つかりませんでした[WHERE" mailboxer_notifications "。" type "=' Mailboxer :: Message 'AND" "receiver_id" =? AND "mailboxer_receipts"。 "receiver_type" =?] 'メッセージを送信すると – Rob