首页 > 解决方案 > 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!

标签: ruby-on-railsrubycheckboxactionmailer

解决方案


这会给一些想法

路线

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

对于卖家

您可以使用上面的表格并更改collectioncollection: @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]


推荐阅读