0

Using Mailboxer to create internal messaging system where user can message another user from a listing of basic profiles (for those who have created them).

The index that shows the list of basic_profiles (experts) is showing, but the link to the create a new message is not passing parameters to create a new message.

Here's the Experts index that lists basic profiles and where the link is for the current user to send a new message to the user who's basic profile it is:

<div style="margin-top:100px;">
 <h3>Experts</h3>
  <% BasicProfile.all.each do |basic_profile| %>
    <div>
      <p style="padding-top:20px;"> <img src="<%= basic_profile.picture_url %>"    
      style="float: left;margin: 5px;"> 
      <span style="font-size:14px;"><b>
      <%= basic_profile.first_name %> <%= basic_profile.last_name %></b></span></><br>
      <b><i><%= basic_profile.headline %></i></b><br>
      <%= basic_profile.location %><br>
      <%= basic_profile.industry %><br>
      <%= basic_profile.specialties %><br>
      <%= button_to "Send a message", new_message_path(@user) %></p>
   </div>
 <% end %>
</div>

Here's the Experts Controller:

class ExpertsController < ApplicationController
  def index
    @basic_profiles = BasicProfile.all
    @user = User.all
  end
end

Here's the Messages Controller:

class MessagesController < ApplicationController

  # GET /message/new
    def new
      @user = User.find_by_email(params[:user])
      @message = current_user.messages.new
    end

  # POST /message/create
   def create
      @recipient = User.find_by_email(params[:user])
      current_user.send_message(@recipient, params[:body], params[:subject])
      flash[:notice] = "Message has been sent!"
      redirect_to :conversations
    end
  end

Here's the view for a new message:

 <div style="margin-top:200px;">

   Send a message to

    <%= @user.email %>
    <%= form_tag({controller: "messages", action: "create"}, method: :post) do %>
    <%= label_tag :subject %>
    <%= text_field_tag :subject %>
    <%= label :body, "Message text" %>
    <%= text_area_tag :body %>
    <%= hidden_field_tag(:user, "#{@user.id}") %>
    <%= submit_tag 'Send message', class: "btn btn-primary" %>
   <% end %>
 </div>

Here's what is in routes for messages:

 resources :messages do
    collection do
      post 'new/:user', to: 'messages#create'
    end
    member do
      post :reply
      post :trash
      post :untrash
      post :new
   end

Devise is being used.

I am wondering why params are not being passed to the view for a new message.

zishe
  • 10,665
  • 12
  • 64
  • 103

1 Answers1

1

Having been posted a month ago, I'm not sure if you figured this out but I stumbled upon this question while trying to solve my own, different, problem with Mailboxer.

I have a similar scenario to yours where I wanted a button to message a user on his page. My view for new message has the same hidden_field as you have.

<%= hidden_field_tag(:user, "#{@user.id}") %>

However, my link_to (where you have the button_to) new message is slightly different. Instead of passing the @user object, I pass the id of the user).

<%= link_to "Message This User", new_message_path(:user => @user.id), class: "btn btn-large btn-primary" %>

Then in the messages_controller, I use find_by(id: instead of the find_by_email you have.

Messages New Action

@user = User.find_by(id: params[:user])

which is what is passed to the hidden_field. Which you can see displayed above, again passes only the id of the user as the :user param to the create action.

From there, we then have the Messages Create Action

@recipient = User.find_by(id: params[:user])
current_user.send_message(@recipient, params[:body], params[:subject])     

Anyway, not sure if this helps but there you go.

FreemanC
  • 27
  • 4