简体   繁体   中英

Adding more send options using the mailboxer gem in rails

I am using the mailboxer gem in rails for an app. It shows how to create a mailbox page in which I can send messages to other users. But I want to know how to add these options for other pages.

For example, a send message button on a user's profile page (to send a message to that user).

Allowing a user to send a message to the poster of a post/story (from the stories gem).

etc..

How do I do this?

The tutorial is https://www.sitepoint.com/messaging-rails-mailboxer/

The tutorial has an addendum that covers most of what you are seeking but adds the button to the user index page. Based on the author's approach here is the answer specifically to your query. This is assuming you finished the original tutorial and it is working fine.

In the messages_controller's 'new' action, create an instance variable called @chosen_recipient that gets the user id from a query string if it is present. The format of a query string following the path is ?key=value so something like localhost:3000/messages/new?to=1.

# app/controllers/messages_controller.rb
def new
  @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
end

Change the new message form's recipients select tag to pass the @chosen_recipient argument to the recipients_options helper method.

# app/views/messages/new.html.erb
<div class="form-group">
  <%= label_tag 'recipients', 'Choose recipients' %>
  <%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
</div>

Modify the recipients_options helper method to preselect the user from the user page as the recipient, passed in as the @chosen_recipient argument.

# app/helpers/messages_helper.rb
def recipients_options(chosen_recipient = nil)
  s = ''
  User.all.each do |user|
    s << "<option value='#{user.id}' data-img-src='{gravatar_image_url(user.email, size: 50)}' 
      #{'selected' if user == chosen_recipient}>#{user.name}</option>"
  end
  s.html_safe
end

Then in your user show page add a button to the new message path passing the user.id as a query string which adds it to the end of the url. This will be picked up by the messages_controller 'new' action created above. Wrap it in an unless conditional to exclude the current_user so they don't end up sending messages to themselves.

# app/views/users/show.html.erb
<% unless current_user == @user %>
  <%= link_to 'Send message', new_message_path(to: @user.id), class: 'btn btn-default btn-sm' %>
<% end %>

And similarly for posts, assuming @post.user is the author:

# app/views/posts/show.html.erb
<% unless current_user == @post.user %>
  <%= link_to 'Send message to author', new_message_path(to: @post.user.id), class: 'btn btn-default btn-sm' %>
<% end %>

Addendum

To add the post title to be the message subject using the same type of approach as above, do the following:

  1. Add the title to the posts/show page link <% = link_to 'Send message', new_message_path(to: @user.id, title: @post.title), class: 'btn btn-default btn-sm' %>

  2. Then in the messages_controller new action add an instance variable @post_title = params[:title]

  3. Then in the messages/new form in the subject field add a value <% = text_field_tag 'message[subject]', nil, class: 'form-control', value: @post_title %>

I figured out how to do this by following the tutorial from Go Rails . I did this by doing this same thing that Steve Carey did by changing my app/controllers/conversations_controller.rb from

 def new @conversation = Mailboxer::Conversation.new @recipients = User.all - [current_user] end def create recipients = User.where(id: params[:user_ids]) receipt = current_user.send_message(recipients, params[:body], params[:subject]) redirect_to conversation_path(receipt.conversation) end 

to...

  def new @conversation = Mailboxer::Conversation.new @recipients = User.all - [current_user] @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to] end def create recipients = User.where(id: params['recipients']) receipt = current_user.send_message(recipients, params[:body], params[:subject]) redirect_to conversation_path(receipt.conversation) end 

and of course by placing the recipients_options helper method and the @chosen_recipient argument in app/helpers/conversations_helper.rb and app/views/conversations/new.html.erb instead of app/helpers/messages_helper.rb and app/views/messages/new.html.erb.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM