简体   繁体   English

在Rails中使用Mailboxer gem添加更多发送选项

[英]Adding more send options using the mailboxer gem in rails

I am using the mailboxer gem in rails for an app. 我正在为应用程序使用railser gem。 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). 允许用户向帖子/故事的发布者发送消息(来自Story gem)。

etc.. 等等..

How do I do this? 我该怎么做呢?

The tutorial is https://www.sitepoint.com/messaging-rails-mailboxer/ 本教程为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. 在messages_controller的“ new”操作中,创建一个名为@chosen_recipient的实例变量,该变量将从查询字符串(如果存在)中获取用户ID。 The format of a query string following the path is ?key=value so something like localhost:3000/messages/new?to=1. 路径后的查询字符串的格式为?key = value,因此类似于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. 更改新邮件表单的收件人选择标记,以将@chosen_recipient参数传递给receives_options帮助器方法。

# 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. 修改receives_options帮助方法,以从用户页面中预先选择用户作为接收者,并以@chosen_recipient自变量传递。

# 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. 然后在用户显示页面中,向新消息路径添加一个按钮,将user.id作为查询字符串传递给它,并将其添加到url的末尾。 This will be picked up by the messages_controller 'new' action created above. 上面创建的messages_controller'new'操作将对此进行处理。 Wrap it in an unless conditional to exclude the current_user so they don't end up sending messages to themselves. 除非有条件,否则将其包装起来以排除current_user,这样他们就不会最终向自己发送消息。

# 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: 同样,对于帖子,假设@ post.user是作者:

# 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' %> 将标题添加到帖子/显示页面链接<%= 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] 然后在messages_controller新动作中添加一个实例变量@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 %> 然后在主题字段的邮件/新表单中,添加一个值<%= text_field_tag'message [subject]',无,类:'form-control',值:@post_title%>

I figured out how to do this by following the tutorial from Go Rails . 通过遵循Go Rails的教程,我找到了解决方法。 I did this by doing this same thing that Steve Carey did by changing my app/controllers/conversations_controller.rb from 我这样做的方法与Steve Carey所做的相同,是通过将我的app / controllers / conversations_controller.rb更改为

 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. 当然也可以通过在app/helpers/conversations_helper.rb and app/views/conversations/new.html.erb /helpers/conversations_helper.rb app/helpers/conversations_helper.rb and app/views/conversations/new.html.erb而不是app/helpers/messages_helper.rb and app/views/messages/new.html.erb.放置receivers_options helper方法和@chosen_recipient参数app/helpers/messages_helper.rb and app/views/messages/new.html.erb.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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