Using the Rails gem 'Mailboxer' and the se

2019-07-26 04:16发布

问题:

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

回答1:

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