在 rails 中使用邮箱 gem 添加更多发送选项
Adding more send options using the mailboxer gem in rails
我在 rails 中使用邮箱 gem 作为应用程序。它展示了如何创建一个邮箱页面,我可以在其中向其他用户发送消息。但是我想知道如何为其他页面添加这些选项。
例如,用户个人资料页面上的发送消息按钮(向该用户发送消息)。
允许用户向 post/story 的发帖人发送消息(来自故事 gem)。
等等
我该怎么做?
该教程有一个附录,涵盖了您正在寻找的大部分内容,但将按钮添加到用户索引页面。根据作者的方法,这里是专门针对您的查询的答案。这是假设您完成了原始教程并且工作正常。
在 messages_controller 的 'new' 操作中,创建一个名为 @chosen_recipient 的实例变量,它从查询字符串中获取用户 ID(如果存在)。路径后面的查询字符串的格式是 ?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
更改新消息表单的收件人 select 标记以将 @chosen_recipient 参数传递给 recipients_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>
修改 recipients_options 帮助程序方法以预先select 用户页面中的用户作为收件人,作为@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
然后在您的用户显示页面中向新消息路径添加一个按钮,将 user.id 作为查询字符串传递,将其添加到 url 的末尾。这将由上面创建的 messages_controller 'new' 操作拾取。将其包装在 unless 条件中以排除 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 %>
对于 posts 也是类似的,假设@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 %>
附录
要使用与上述相同类型的方法将 post 标题添加为邮件主题,请执行以下操作:
将标题添加到 posts/show 页面 link <% = link_to 'Send message', new_message_path(to: @user.id, 标题: @post.title), class: 'btn btn-default btn-sm' %>
然后在messages_controller新动作中添加一个实例变量
@post_title = 参数[:标题]
然后在messages/new表单的subject字段中添加一个值<%=text_field_tag'message[subject]',nil,class:'form-control', 值: @post_title %>
我按照 Go Rails 中的教程了解了如何执行此操作。我这样做的方法与 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
到...
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
当然,通过将 recipients_options 辅助方法和 @chosen_recipient 参数放在 app/helpers/conversations_helper.rb and app/views/conversations/new.html.erb
而不是 app/helpers/messages_helper.rb and app/views/messages/new.html.erb.
中
我在 rails 中使用邮箱 gem 作为应用程序。它展示了如何创建一个邮箱页面,我可以在其中向其他用户发送消息。但是我想知道如何为其他页面添加这些选项。
例如,用户个人资料页面上的发送消息按钮(向该用户发送消息)。
允许用户向 post/story 的发帖人发送消息(来自故事 gem)。
等等
我该怎么做?
该教程有一个附录,涵盖了您正在寻找的大部分内容,但将按钮添加到用户索引页面。根据作者的方法,这里是专门针对您的查询的答案。这是假设您完成了原始教程并且工作正常。
在 messages_controller 的 'new' 操作中,创建一个名为 @chosen_recipient 的实例变量,它从查询字符串中获取用户 ID(如果存在)。路径后面的查询字符串的格式是 ?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
更改新消息表单的收件人 select 标记以将 @chosen_recipient 参数传递给 recipients_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>
修改 recipients_options 帮助程序方法以预先select 用户页面中的用户作为收件人,作为@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
然后在您的用户显示页面中向新消息路径添加一个按钮,将 user.id 作为查询字符串传递,将其添加到 url 的末尾。这将由上面创建的 messages_controller 'new' 操作拾取。将其包装在 unless 条件中以排除 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 %>
对于 posts 也是类似的,假设@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 %>
附录
要使用与上述相同类型的方法将 post 标题添加为邮件主题,请执行以下操作:
将标题添加到 posts/show 页面 link <% = link_to 'Send message', new_message_path(to: @user.id, 标题: @post.title), class: 'btn btn-default btn-sm' %>
然后在messages_controller新动作中添加一个实例变量 @post_title = 参数[:标题]
然后在messages/new表单的subject字段中添加一个值<%=text_field_tag'message[subject]',nil,class:'form-control', 值: @post_title %>
我按照 Go Rails 中的教程了解了如何执行此操作。我这样做的方法与 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
到...
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
当然,通过将 recipients_options 辅助方法和 @chosen_recipient 参数放在 app/helpers/conversations_helper.rb and app/views/conversations/new.html.erb
而不是 app/helpers/messages_helper.rb and app/views/messages/new.html.erb.