Search code examples
ruby-on-railsmethodscontrollers

NoMethodError in Flights#show


Showing app/views/flights/show.html.erb where line #3 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name

Extracted source (around line #3):

1: <p>
2:  <b>Airline Name:</b>

3:  <%= @flight.airline.name %>

well the above error is occurring when am trying to submit the 'seat' form.

I have a '_new_seat' 'partial' page in my flights/show. when clicking on 'book new seat'(submit), it should redirect to seats/show, but it doesn't..

whether it requires an extra method in flights controller?.. please help me.


Solution

  • Your @flight does not have an airline associated.

    To get off the error when there is no airline associated, write:

    @flight.airline.try(:name)
    

    To have an airline associated. check your @flight object properties.

    First, check you have this:

    class Flight < ActiveRecord::Base
      belongs_to :airline
    

    Second, you have airline_id correctly set in flight instance.

    Third, you have an airline with that airline_id saved in airlines table.