Rahul Poddar
Rahul Poddar

Reputation: 343

How to solve NoMethodError for this case?

So my rails app has a number of airport fares defined for each operator, as:

class AirportFare < ActiveRecord::Base
    belongs_to :operator
end

class Operator < ActiveRecord::Base
    has_many :airport_fares
end

My routes are defined as:

resources :operators do
    resources :airport_fares
end

And following is my airport_fares_controller:

class AirportFaresController < ApplicationController
  before_action :set_airport_fare, only: [:show, :edit, :update, :destroy]
  before_action :set_operator

  # GET /airport_fares
  # GET /airport_fares.json
  def index
    @operator = Operator.find(params[:operator_id])
    @airport_fares = Operator.find(params[:operator_id]).airport_fares
  end

  # GET /airport_fares/1
  # GET /airport_fares/1.json
  def show
  end

  # GET /airport_fares/new
  def new
    @airport_fare = Operator.find(params[:operator_id]).airport_fares.build
  end

  # GET /airport_fares/1/edit
  def edit
  end

  # POST /airport_fares
  # POST /airport_fares.json
  def create
    @airport_fare = Operator.find(params[:operator_id]).airport_fares.build(airport_fare_params)

    respond_to do |format|
      if @airport_fare.save
        format.html { redirect_to @airport_fare, notice: 'Airport fare was successfully created.' }
        format.json { render :show, status: :created, location: @airport_fare }
      else
        format.html { render :new }
        format.json { render json: @airport_fare.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /airport_fares/1
  # PATCH/PUT /airport_fares/1.json
  def update
    respond_to do |format|
      if @airport_fare.update(airport_fare_params)
        format.html { redirect_to @airport_fare, notice: 'Airport fare was successfully updated.' }
        format.json { render :show, status: :ok, location: @airport_fare }
      else
        format.html { render :edit }
        format.json { render json: @airport_fare.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /airport_fares/1
  # DELETE /airport_fares/1.json
  def destroy
    @airport_fare.destroy
    respond_to do |format|
      format.html { redirect_to operator_airport_fares_path(operator_id: @operator.id), notice: 'Airport fare was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_operator
      @operator = Operator.find(params[:operator_id])
    end

    def set_airport_fare
      @airport_fare = AirportFare.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def airport_fare_params
      params.require(:airport_fare).permit(:cabcat, :triptype, :triptime, :basedist, :basehr, :basechrg, :ratepkm, :waitingbtime, :waitingchrg, :notes, :operator_id)
    end
end

My db schema says:

create_table "airport_fares", force: true do |t|
    t.string   "cabcat"
    t.string   "triptype"
    t.string   "triptime"
    t.decimal  "basedist",     precision: 8, scale: 2
    t.integer  "basehr"
    t.decimal  "basechrg",     precision: 8, scale: 2
    t.decimal  "ratepkm",      precision: 8, scale: 2
    t.integer  "waitingbtime"
    t.decimal  "waitingchrg",  precision: 8, scale: 2
    t.text     "notes"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "operator_id"
  end

The problem is when I try this in my console: AirportFare.first.operator. This leads to the error:

NoMethodError: undefined method 'operator' for #<AirportFare>. 

Where is the problem?

Upvotes: 1

Views: 90

Answers (2)

Shatabdi Mohapatra
Shatabdi Mohapatra

Reputation: 467

In new action you should do:

 def new
     @operator = Operator.find(params[:operator_id])
     @airprot_fare = AirportFare.new
 end

in your form:

<%= form_for [@operator, @airport_fare] do |f| %>

This should work.

Upvotes: 1

Hemali
Hemali

Reputation: 465

It Should be operator.first.airport_fares. As you have has_many relationship on operators

Upvotes: 0

Related Questions