I recently added Mailboxer
to my Rails app and have everything working well.
However, I want to be able to create a new message by clicking a "Message Me" button on a User's page and then have the recipient filled in already (with the User from the prior page) when the form for creating the message loads.
The particular field in question:
<%= select_tag 'recipients', recipients_options, multiple: true, class: 'new_message_recipients_field' %>
And for reference, recipients_options
probably won't come into play in this scenario, but it is the list of all Users--which I use primarily when I'm creating a Message from anywhere but a User page--and is defined in my Messages helper
def recipients_options
s = ''
User.all.each do |user|
s << "<option value='#{user.id}'>#{user.first_name} #{user.last_name}</option>"
end
s.html_safe
end
And my messages_controller.rb
:
def new
end
def create
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 conversation_path(conversation)
end
I already have the link to the New Message form on each User page...
<%= link_to 'Message me', new_message_path %>
But I don't know how to pre-fill the recipient in the subsequent form.
Any suggestions for how would I go about this?
Change the link_to
code to this:
<%= link_to 'Message me', new_message_path(recipient_id: @user.id) %>
In messages#new
action, add this code:
def new
@users = User.all
end
And, in message form, replace the select_tag with this:
<%= select_tag 'recipients', options_from_collection_for_select(@users, :id, :first_name, params[:recipient_id]), multiple: true, class: 'new_message_recipients_field' %>
I made some changes in recipients select. You can use options_from_collection_for_select
method to generate that options instead implement your own helper.
Reference: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
I am using Mailboxer and I'm able to, from a Listing page, contact the seller.
The user clicks on the contact seller button on Listing page and in on next page the To: form is filled out with seller's name.
I have the same Create method as in your message controller. but in NEW method i have this:
MessageController
def new
@chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
end
The contact button on my listing page:
</p>
<strong>Contact Seller about "<%= @listing.title %>"</strong></br>
<%= link_to "Send message to #{@listing.user.name}", new_message_path(to: @listing.user.id), class: 'btn btn-default btn-sm' %>
<p>
The NEW message form:
<%= form_tag 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 %>
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.