简体   繁体   English

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

[英]How can I have the recipient field default to specific users?

QUESTION: How can I have the recipient field, automatically filled out, with a user's username, for example, using the MailBoxer gem? 问题:如何使用用户名(例如,使用MailBoxer gem)自动填写收件人字段?

I have a user admin account, and I want to have the recipient field pre-filled out, when I click on a user (non admin) profile page, in the show.html.erb view. 我有一个用户管理员帐户,当我在show.html.erb视图中单击用户(非管理员)个人资料页面时,希望预先填写收件人字段。

Currently, the code used displays ALL users utilising the platform, whereas I want this specific form to only display the recipient. 当前,使用的代码显示使用该平台的所有用户,而我希望此特定表格仅显示收件人。

I am using the mailboxer gem, and I have the subject and message field already pre-filled and working. 我正在使用邮箱管理器gem,并且我的主题和消息字段已经预先填写并可以正常工作。

The code for the form, which is in the show.html.erb view: 表单的代码位于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 %>

you can use attribute selected :selected => @user.id (based your code above you have @user) 您可以使用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>

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

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