0

Trying to create a Sale that has a join table to Events and establish a relationship when it is created from a dropdown list of all available events.

<%= 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 %>

My model:

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

My controller:

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

Can't seem to get it to work.

  • You have to show a little more, including your error notices or the log. I doubt you need a "join table" but probably you are missing a key relationship. You have "events" as a param in the deals table? Lots of folks here will try to help if you supply enought information. – Tom Connolly Feb 22 '17 at 14:49
  • Just edited the question with my latest variant. What I really have is Sale that has_one :event and Events that has_many :sales, this all happens in a join table called sales_events. All I want to do after is also pass in an event when I create a sale. This event I select from a dropdown of existing events. Edited the question to show this and marked confusing regions with ??? –  Feb 22 '17 at 18:49

0 Answers0