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

时间:2017-02-22 14:35:37

标签: 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 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 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

似乎无法让它发挥作用。

0 个答案:

没有答案