所以我的rails应用程序为每个运营商定义了许多机场票价,如:
class AirportFare < ActiveRecord::Base
belongs_to :operator
end
class Operator < ActiveRecord::Base
has_many :airport_fares
end
我的路线定义为:
resources :operators do
resources :airport_fares
end
以下是我的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
我的数据库架构说:
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
问题出在我在我的控制台中尝试此问题时:AirportFare.first.operator
。这导致错误:
NoMethodError: undefined method 'operator' for #<AirportFare>.
问题出在哪里?
答案 0 :(得分:1)
在新的行动中你应该这样做:
def new
@operator = Operator.find(params[:operator_id])
@airprot_fare = AirportFare.new
end
在您的表单中:
<%= form_for [@operator, @airport_fare] do |f| %>
这应该有用。
答案 1 :(得分:0)
应该是operator.first.airport_fares。因为你有关于运营商的has_many关系