title: Nested Resources - No Method Error
body: <p>I'm creating a ticket booking app as my sample project using Ruby on Rails 4.1. Three are three models - Events, Tickets and Bookings. Events have many tickets and bookings. Tickets have many bookings and they belong to events. Bookings belongs to events and tickets.</p>
  
  <p>The routes file looks like:</p>
  
  <pre><code>Rails.application.routes.draw do
  
  resources :charges
  
  root 'events#index'
  
  resources :events do
  resources :tickets
  resources :bookings
  end
  end
  </code></pre>
  
  <p>The buttons to the new booking page are located in my events/show page which has the following code:</p>
  
  <pre><code>&lt;% @event.tickets.each do |ticket| %&gt;
   &lt;div class="row"&gt;
  &lt;div class="col-md-8"&gt;
    &lt;table class="table"&gt;
      &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;Name&lt;/th&gt;
                &lt;th&gt;Price&lt;/th&gt;
                &lt;th&gt;Quantity&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;%= ticket.ticket_name %&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%= ticket.ticket_price %&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%= ticket.ticket_quantity %&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%= link_to "Buy Now", new_event_booking_path(@event, ticket), class: "btn btn-primary" %&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%= link_to "Edit", edit_event_ticket_path(@event, ticket), class: "btn btn-link" %&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%= link_to "Delete", event_ticket_path(@event, ticket), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-link" %&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  
  
  
  
  <p>As you can see, the event and ticket variables are grabbed when the user clicks the "Buy Now" button. However, when I use the following code in my Bookings controller:</p>
  
  <pre><code>class BookingsController &lt; ApplicationController
  
  def new
  @event = Event.find(params[:event_id])
  @ticket = @event.tickets.find(params[:id])
  @booking = Booking.new
  end
  
  def create
  @event = Event.find(params[:event_id])
  @ticket = @event.tickets.find(params[:id])
  @booking = @event.bookings.create(booking_params)
  
  if @booking.save
      redirect_to [@event, @booking]
  else
      render 'new'
  end
  end
  
  def show
  @event = Event.find(params[:event_id])
  @booking = @event.bookings.find(params[:id])
  end
  
    private
    def booking_params
  params.require(:booking).permit(:buyer_name, :email, :mobile, :address, :order_quantity)
    end
  end
  </code></pre>
  
  <p>I get the <strong>ActiveRecord::RecordNotFound in BookingsController#new. Couldn't find Ticket without an ID</strong> error.</p>
  
  <p>The new page looks like this (pretty barebones as I testing the functionality)</p>
  
  <pre><code>&lt;%= form_for ([@event, @booking]) do |f| %&gt;
   &lt;div class="row"&gt;
    &lt;div class="form-group"&gt;
      &lt;%= f.label :buyer_name %&gt;
      &lt;%= f.text_field :buyer_name, class: "form-control" %&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;%= f.label :order_quantity %&gt;
      &lt;%= f.text_field :order_quantity, class: "form-control" %&gt;
    &lt;/div&gt;
  
  &lt;/div&gt;
    &lt;%= f.submit "Pay now", class: "btn btn-primary" %&gt;
  </code></pre>
  
    
  
  <p>The Events Model:</p>
  
  <pre><code>has_many :tickets, dependent: :destroy
  has_many :bookings
  has_many :charges
  </code></pre>
  
  <p>The Tickets model:</p>
  
  <pre><code>belongs_to :event
  has_many :bookings
  </code></pre>
  
  <p>The Bookings model:</p>
  
  <pre><code>belongs_to :event
  belongs_to :ticket
  has_many :charges
  </code></pre>
  
  <p>The foreign key association looks like this:</p>
  
  <pre><code>add_reference :bookings, :ticket, index: true
  </code></pre>
  
  <p>How to solve this error?  </p>