ruby-on-rails - Selecting a nested record to share show page of with selected users in action mailer via checkbox selection
问题描述
So with the devise gem set up I have, my users can register as either a buyer account or a seller account. If they're buyers, they can make nested inquiries for information to buy from sellers. On the sellers index page, if I am a buyer and logged in as the current user, I want every inquiry that I've made to be displayed on a part of the page with the title of each inquiry displayed next to a radio button or as an option from a pull down menu for the buyer to select from, along with the inquiry ID hidden so I can use it in action mailer after I select it. I am guessing I need a form with a post request to select the inquiry I've done for the next step, so this is what I've been trying so far:
<%= simple_form_for [@buyer, @inquiries], method: :post do |f| %>
<%= f.input :inquiry.name, collection: @inquiries, as: :radio_buttons %>
<%= f.text_field :inquiry_id, value: inquiry.id, type: "hidden" %>
<%= f.submit 'New user' %>
In my sellers index controller, for the buyer and inquiry I have
@buyer = current_user.buyer if current_user.buyer
@inquiries = @buyer.inquiries if current_user.buyer
However, I haven't been able to properly get my list of inquiries to show with a hidden ID either as a pull down menu of options or radio button options.
Afterwards, on the same seller index page, I have checkboxes on every individual seller partial card. As a buyer, I want to be able to use action mailer to send every seller whose checkbox I clicked on an email inviting them to see a link to the show page of the inquiry that I've picked, all with the click of a button. I haven't gotten close to getting this to work. So far the checkbox I have on each seller partial so I can have their ID's hidden is this:
<%= check_box("seller", "id", {}, seller.id) %>
This is my inquiry mailer file:
class InquiryMailer < ApplicationMailer
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.inquiry_mailer.invite.subject
#
def invite(seller)
# @greeting = "Hi"
@buyer = current_user.buyer
@inquiries = @buyer.inquiries
@seller = seller
mail(to: @seller.email, subject: 'You have been invited to sell your!')
end
end
And this is my invite.html.erb file:
<p>Hello, <%= @seller.email %>!</p>
<p>You have been invited to sell to <%=@buyer.name%>!</p>
<p>Their inquiry is titled <%= @inquiry.title %>, it can be found at<p>
<p><%= link_to 'See this inquiry!', buyer_inquiry_path(@buyer, @inquiry) %><p>
But I'm not sure if what I have so far is right and where to go from here to get my next steps to work. I do not know how to transmit the inquiry ID to action mailer, nor do I know how to send the selected checkboxes properly either. I'd be grateful for any advice on what steps I should take next to get my inquiries to show the proper way and how to send the inquiry that I pick to the sellers that I pick. Thank you!
解决方案
这会给一些想法
路线
resources :sellers do
collection { post :notify }
end
形式
<%= simple_form_for :inquiry, url: notify_sellers_path do |f| %>
<%= f.input :ids, collection: @inquiries, as: :check_boxes, label: false%>
<%= f.submit %>
这将为每个查询创建复选框(或者您可以将其更改为多选)
在提交时它将提交inquiry[:ids]
给notify
方法SellersController
在那里您可以访问 IDparams[:inquiry][:ids]
并创建通知
def notify
inquiry_ids = params[:inquiry][:ids]
do stuff
InquiryMailer.notify_seller(params[:inquiry][:ids])
...
end
对于卖家
您可以使用上面的表格并更改collection
为collection: @sellers
或
<%= simple_form_for :seller, url: notify_sellers_path do |f| %>
<% @sellers.each do |seller| %>
<%= f.check_box :ids, { multiple: true }, seller.id, nil %>
<%= seller.name %>
...
这将提交选定的 ID 作为params[:seller][:ids]
推荐阅读
- python - 如何将字符串附加到数据框中的每个后续行?
- c++ - 使用 VS2017 构建 GLUI 时出现 C2252 错误?
- android - 如何在不更改其他布局的情况下更改 RecyclerView 中的单个项目
- php - 使用 $_SESSION php 的多级导航栏
- asp.net - Asp.net Webforms:为 typeform 创建 webhook 接收器
- powershell - 使用 FileSystemWatcher 在多个文件同时更改时发出警报
- python - Holoviews 热图 X 轴格式
- asp.net-mvc - .net 4.7 中的依赖注入?
- javascript - 如何使用 vanilla JavaScript 更改此 SVG 精灵的 href?
- python - 如何将 tkinter 按钮与标签和输入框保持在同一行