0

我有一个名为 Order 的多重关系表,它属于一个 Relai(以避免单数/复数并发症)、一个客户和一个组合。我使用下面提到的nested_attributes 相应地设置了我的订单模型。在添加客户部分之前,我想发送一封邮件,其中只包含@composition 和一个带有下拉列表的@relai。

class Order < ApplicationRecord
  belongs_to :relai
  belongs_to :composition
  accepts_nested_attributes_for :relai
  accepts_nested_attributes_for :composition
end

我将我的 OrdersController 设置为从我的参数中获取 :composition_id

def new
    @order = Order.new
    @composition = Composition.find(params[:composition_id])
    @relais = Relai.all
  end

  def create
    @florist = Florist.first
    @composition = Composition.find(params[:composition_id])
    #@relai = Relai.find(params[:relai_id])  # If I add this line it returns "Couldn't find Relai without an ID"
    @order = Order.new(order_params)
    if @order.save!
      raise
      OrderMailer.order_mail(@order).deliver
      redirect_to thanks_purchase_path
    else
      render :new
    end
  end

  private

  def order_params
    params.require(:order).permit(
      florist_attributes: [:id],
      relai_attributes: [:id, :name, :address],
      composition_attributes: [:id, :name]
      )
  end

我的观点:

  <%= form_with(model: @order, url: composition_orders_path(@composition), local: true) do |compo| %>
    <%= compo.collection_select :relai, @relais, :id, :name %>
    <%= compo.fields_for :composition do |fc| %>
      <%= fc.collection_select :composition, @compositions, :id, :name %>
    <% end %>
    # Start Block that doesn't display the Relai.all
    #<%#= compo.fields_for :relai do |fr| %>
     #<%#= fr.label :relai, 'Ici la liste des relais :' %>
     #<%#= fr.association :relai, collection: @relais %>
     #<%#= fr.association :relai, @relais, :id, :name %>
    #<%# end %>
    # End Block
    <%= compo.submit "MAIL", class: "app-form-button" %>
  <% end %>

和路线:

resources :compositions, only: [:show, :index] do
    resources :orders, only: [:show, :new, :create, :index]
  end

我也试过:

有人能解释一下为什么我得到“验证失败:Relai 必须存在,Composition 必须存在”,而这些出现在我的参数中吗?

{"authenticity_token"=>"[FILTERED]", "order"=>{"relai"=>"2"}, "commit"=>"MAIL", "composition_id"=>"3"}

我在 Rails 6.1.4 上;红宝石 2.6.6

4

1 回答 1

0

accepts_nested_attributes_for从父母到孩子的作品。您在子端 ( Order) 使用它。

如果您只需要分配一个现有的Relai并且CompositionOrder为它们两个使用一个选择:

class Order < ApplicationRecord
  belongs_to :relai
  belongs_to :composition
end
  def new
    @order = Order.new
    @compositions = Composition.all
    @relais = Relai.all
  end

  def create
    @order = Order.new(order_params)
    if @order.save!
      OrderMailer.order_mail(@order).deliver
      redirect_to thanks_purchase_path
    else
      render :new
    end
  end

  private

  def order_params
    params.require(:order).permit(:relai_id, :composition_id)
  end  
<%= form_with(model: @order, url: composition_orders_path(@composition), local: true) do |compo| %>
    <%= compo.collection_select :relai_id, @relais, :id, :name %>
    <%= compo.collection_select :composition_id, @compositions, :id, :name %>
    <%= compo.submit "MAIL", class: "app-form-button" %>
<% end %>

编辑:在控制器上设置组合。

  def new
    composition = Composition.find(params[:composition_id])
    @order = Order.new(composition: composition)    
    @relais = Relai.all
  end

  def create
    @order = Order.new(order_params)
    if @order.save!
      OrderMailer.order_mail(@order).deliver
      redirect_to thanks_purchase_path
    else
      render :new
    end
  end

  private

  def order_params
    params.require(:order).permit(:relai_id, :composition_id)
  end  
<%= form_with(model: @order, url: composition_orders_path(@composition), local: true) do |compo| %>
    <%= compo.collection_select :relai_id, @relais, :id, :name %>
    <%= compo.hidden_field :composition_id %>
    <%= compo.submit "MAIL", class: "app-form-button" %>
<% end %>
于 2021-10-29T06:48:24.060 回答