如何使收件人字段默认为特定用户?

时间:2019-01-24 00:34:42

标签: ruby-on-rails ruby mailboxer

问题:如何使用用户名(例如,使用MailBoxer gem)自动填写收件人字段?

我有一个用户管理员帐户,我想在show.html.erb视图中单击用户(非管理员)个人资料页面时预先填写收件人字段。

当前,使用的代码显示使用该平台的所有用户,而我希望这种特定形式仅显示收件人。

我正在使用mailer gem,并且我的主题和消息字段已经预先填写并可以正常工作。

表单的代码,位于show.html.erb视图中:

<%= form_for :conversation, url: :conversations, html: { class: "" } do |f| %>

  <div class="form-group">
    <%= f.label :recipients %>
    <%= f.select(:recipients, User.all.collect {|p| [ p.username, p.id ] }, {}, { multiple: true , class: "chosen-select form-control", :data => {:placeholder => "Please type in a username to search for a user"}})%>
  </div>

  <div class="form-group">
    <%= f.label :subject %>
    <%= f.text_field :subject, placeholder: "Subject", class: "form-control",
        value: "Hi #{@user.username}! %>
  </div>

  <div class="form-group">
    <%= f.label :message %>
    <%= f.text_area :body, class: 'form-control',placeholder: "Type your message here", rows: 4,
        value:

"Hi #{@user.username}!

I am mailing you to tell you that [INSERT ITEM NAME] is in stock.

Please come down to receive the item. %>
  </div>

  <%= f.submit "Send Message" %>

<% end %>

1 个答案:

答案 0 :(得分:1)

您可以使用selected属性:selected => @ user.id(基于您具有@user的代码)

  <div class="form-group">
    <%= f.label :recipients %>
    <%= f.select(:recipients, User.all.collect {|p| [ p.username, p.id ] }, {},  , { :selected => @user.id, multiple: true , class: "chosen-select form-control", :data => {:placeholder => "Please type in a username to search for a user"}})%>
  </div>