2015-09-02 53 views
0

我知道這是一個非常普遍的問題,但我無法解決任何其他主題。 這是問題:未定義的局部變量或方法`停止'爲#<#<Class:0xb085c0a4>:0xb16e8c6c>

未定義局部變量或方法`停止」的#<#:0xb16e8c6c>

應用/視圖/停止/ show.html.erb

<p id="notice"><%= notice %></p> 

<p> 
    <strong>City name:</strong> 
    <%= @stop.city %> 
</p> 

<p> 
    <strong>Arrival time:</strong> 
    <%= @stop.Arrival_time %> 
</p> 

<p> 
    <strong>Route:</strong> 
    <%= @stop.Route_id %> 
</p> 

<%= link_to 'Edit', edit_stop_path(@stop) %> | 
<%= link_to 'Back', stops_path %> 

db/schema.rb

create_table "stops", force: :cascade do |t| 
    t.time  "Arrival_time" 
    t.integer "Route_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "city" 
    end 

應用程序/控制器/ stops_controller

class StopsController < ApplicationController 
    before_action :set_stop, only: [:show, :edit, :update, :destroy] 

    # GET /stops 
    # GET /stops.json 
    def index 
    @stops = Stop.all 
    end 

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

    # GET /stops/new 
    def new 
    @stop = Stop.new 
    end 

    # GET /stops/1/edit 
    def edit 
    end 

    # POST /stops 
    # POST /stops.json 
    def create 
    @stop = Stop.new(stop_params) 

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

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

    # DELETE /stops/1 
    # DELETE /stops/1.json 
    def destroy 
    @stop.destroy 
    respond_to do |format| 
     format.html { redirect_to stops_url, notice: 'Stop was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_stop 
     @stop = Stop.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def stop_params 
     params.require(:stop).permit(:city, :Arrival_time, :Route_id) 
    end 
end 

我看不出是哪裏的問題..你可以幫助我嗎?謝謝

回答

1

您會看到此錯誤,因爲@stop未在展示頁面上定義。您可以通過在停止控制器中的show方法中定義它來解決此問題。

我假設顯示頁面將傳遞商店ID的參數。如果是這樣,你可以這樣定義它:

def show 
    @stop = Stop.find(params[:id]) 
end 
+0

謝謝老兄,這個作品完美! – Pino

相關問題