在我的应用程序中,我有一个列表页面,用户可以在其中显示他们的产品。
其他用户可以联系上市作者。
我在列表页面上创建了一个消息按钮,一旦点击该页面,就会重定向到Mailboxer表单并预填充收件人文本字段。我希望主题填写列表标题的文本。
我的代码:
module MessagesHelper
# This is the helper for grabbing a User
def recipients_options(chosen_recipient = nil)
s = ''
User.all.each do |user|
s << "<option value='#{user.id}' #{'selected' if user == chosen_recipient}>#{user.name}</option>"
end
s.html_safe
end
# This is the helper for grabbing the Title from Listing
def recipient_subject(chosen_subject = nil)
s = ''
Listing.all.each do |subject|
s << "<option value='#{subject.id}' #{'selected' if subject == chosen_subject}>#{subject.title}</option>"
end
s.html_safe
end
end
我的控制器
class MessagesController < ApplicationController
before_action :logged_in_user
# before_action :correct_user
def new
@chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
@chosen_subject = Listing.find_by(title: params[:title].to_i) if params[:title]
end
def create
recipients = User.where(id: params['recipients'])
conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
flash[:success] = "Message has been sent!"
redirect_to conversation_path(conversation)
end
end
我的观点
清单&gt;显示(要联系的按钮)
<p>
<strong>Contact Seller about "<%= @listing.title %>"</strong></br>
<%= link_to "Send message to #{@listing.user.name}", new_message_path(to: @listing.user.id, message: @listing.title), class: 'btn btn-default btn-sm' %>
</p>
最后我的新消息表格
<%= form_tag messages_path, method: :post do %>
<div class="form-group">
<%= label_tag 'message[subject]', 'Subject' %>
<%= text_field_tag 'message[subject]', recipient_subject, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= label_tag 'message[body]', 'Message' %>
<%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= label_tag 'recipients', 'Choose recipients' %>
<%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
</div>
<%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>
使用此代码,所有标题都会显示在主题字段中列出:
<option value='5' >New Samsung</option><option value='4' >Title</option><option value='3' >New listing</option><option value='2' >Something</option><option value='1' >Disco</option>
我希望该单一商家信息的标题出现,但它对我不起作用;它显示所有内容以及选项和价值代码。在此先感谢您的帮助!