未定义的局部变量或方法`stop'用于#&lt;#<class:0xb085c0a4>:0xb16e8c6c&gt;

时间:2015-09-02 10:54:00

标签: ruby-on-rails

我知道这是一个非常常见的问题,但我无法通过任何其他主题解决此问题。 这就是问题所在:

未定义的局部变量或方法`stop'for#&lt;#:0xb16e8c6c&gt;

应用/视图/停止/ 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 %>

分贝/ 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 个答案:

答案 0 :(得分:1)

您收到此错误是因为未在展示页面上定义@stop。您可以通过在Stop Controller中的show方法中定义它来解决此问题。

我假设展示页面会传递商店ID的参数。如果是这样,您可以这样定义:

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