Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 创建使用联接表连接到另一个模型对象的新模型对象?_Ruby On Rails_Has And Belongs To Many - Fatal编程技术网

Ruby on rails 创建使用联接表连接到另一个模型对象的新模型对象?

Ruby on rails 创建使用联接表连接到另一个模型对象的新模型对象?,ruby-on-rails,has-and-belongs-to-many,Ruby On Rails,Has And Belongs To Many,尝试创建一个销售,该销售具有与事件的联接表,并在从所有可用事件的下拉列表中创建时建立关系 <%= form_for(@deal) do |f| %> <% if @deal.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@deal.errors.count, "error") %> prohibited this deal from b

尝试创建一个销售,该销售具有与事件的联接表,并在从所有可用事件的下拉列表中创建时建立关系

<%= form_for(@deal) do |f| %>
  <% if @deal.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@deal.errors.count, "error") %> prohibited this deal from being saved:</h2>

      <ul>
        <% @deal.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :event??? %><br>
    <%= f.collection_select :event???, @events.all, :id???, :date???, { promt: "Event Date" } %>

  </div>
  <div class="field">
    <%= f.label :quantity %><br>
    <%= f.number_field :quantity %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div>
  <div class="actions">
    <%= f.submit I18n.t('views.save') %>
  </div>
<% end %>

禁止保存此交易:



我的模型:

class Sale < ActiveRecord::Base

  has_one :event, :through => :deals_events
  accepts_nested_attributes_for :???

end
class Event < ActiveRecord::Base

  has_many :sales, :through => :deals_events

end
class Sale:交易\u事件
接受以下对象的嵌套属性:???
结束
类事件:交易\u活动
结束
我的控制器:

class DealsController < ApplicationController
  def create
    @deal = Deal.new(deal_params)

    respond_to do |format|
      if @deal.save
        format.html { redirect_to @deal, notice: 'Deal was successfully created.' }
        format.json { render :show, status: :created, location: @deal }
      else
        format.html { render :new }
        format.json { render json: @deal.errors, status: :unprocessable_entity }
      end
    end
  end
    def deal_params
      params.require(:deal).permit(:quantity, :price, event_attributes: [ :id, :date ])
    end
end
class DealsController

似乎无法使其工作。

您必须显示更多信息,包括错误通知或日志。我怀疑您是否需要“连接表”,但可能您缺少一个关键关系。您在deals表中将“events”作为参数?如果你提供足够的信息,这里的很多人都会尽力帮助你。只是用我的最新版本编辑了这个问题。我真正拥有的是包含一个:事件的Sale和包含多个:sales的事件,所有这些都发生在一个名为sales\u Events的联接表中。之后我想做的就是在我创建销售时通过一个活动。我从现有事件下拉列表中选择此事件。编辑问题以显示此内容,并用???