Search code examples
ruby-on-railstypeerrornested-attributesform-foractioncontroller

ActionController “No explicit conversion of Symbol into Integer” for new record in Rails 4.2.1


I'm trying to create @booking and @booking.build_passenger in form_for with nested attributes in Rails 4.2.1

The error I get: enter image description here

As you see in the console at the bottom of the image:
1. params.require(:booking) returns a Hash-like params for @booking
2. params.class returns ActionController::Parameters

As the params seems to behave correctly, IMO the problem hides somewhere in the form:

<%= form_for @booking do |f| %>

    <%= f.hidden_field :flight_id, value: params[:flight_id] %>

    <%= render 'flights/flight_info' %>

    <div class="field">
        <b><%= f.label :num_tickets, "Tickets" %></b>  
        <%= f.select(:num_tickets, @num_tickets) %>
    </div><br>
  
    <h4>Passenger info:</h4>
    <%= f.fields_for @booking.build_passenger do |pass| %>
        <div class="field">
            <%= pass.label :name %>
            <%= pass.text_field :name %>
        </div>
        <div class="field">
            <%= pass.label :email %>
            <%= pass.email_field :email %>
        </div>
    <% end %>
    <%= f.submit 'Book Flight!' %>
<% end %>

Booking model:

class Booking < ActiveRecord::Base
  belongs_to :flight
  belongs_to :passenger
  accepts_nested_attributes_for :passenger
end

Question: Where and how do I have to edit my code for the app to start creating @booking instances + @booking.build_passenger()


Solution

  • Your booking_params needs to be something like:

    def booking_params
      params.require(:booking).permit(:flight_id, :num_tickets, passenger_attributes: [:id, :name, :email])
    end