ruby-on-railsrubymailboxer

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?

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.

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.

The code for the form, which is in the show.html.erb view:

<%= 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 %>

Solution

  • you can use attribute selected :selected => @user.id (based your code above you have @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>