0

我有这个控制器:

class AlexesController < ApplicationController
  # GET /alexes
  # GET /alexes.json
  def index
    #@alexes = Alex.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @alexes }
    end
  end

  # GET /alexes/1
  # GET /alexes/1.json
  def show
  #  @alex = Alex.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @alex }
    end
  end

  # GET /alexes/new
  # GET /alexes/new.json
  def new
    @alex = Alex.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @alex }
    end
  end

  # GET /alexes/1/edit
  def edit
    @alex = Alex.find(params[:id])
  end

  # POST /alexes
  # POST /alexes.json
  def create
    @alex = Alex.new(params[:alex])

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

  # PUT /alexes/1
  # PUT /alexes/1.json
  def update
    @alex = Alex.find(params[:id])

    respond_to do |format|
      if @alex.update_attributes(params[:alex])
        format.html { redirect_to @alex, notice: 'Alex was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @alex.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /alexes/1
  # DELETE /alexes/1.json
  def destroy
    @alex = Alex.find(params[:id])
    @alex.destroy

    respond_to do |format|
      format.html { redirect_to alexes_url }
      format.json { head :no_content }
    end
  end
end

按下此链接时会调用它:

<%= link_to "Alex Link", alexes_path(@alex) %>

所以我假设控制器的 get-all 部分会被调用,我在控制器中注释掉了我认为会被调用的行,但我仍然得到这个错误:

undefined method `each' for nil:NilClass

从第 10 行开始:

7:     <th></th>
8:   </tr>
9: 
10: <% @alexes.each do |alex| %>
11:   <tr>
12:     <td><%= link_to 'Show', alex %></td>
13:     <td><%= link_to 'Edit', edit_alex_path(alex) %></td>

知道问题出在哪里吗?

谢谢!

4

1 回答 1

1
<%= link_to "Alex Link", alexes_path(@alex) %>

=>

<%= link_to "Alex Link", alex_path(@alex) %>

或者

<%= link_to "Alex Link", @alex %>
于 2012-04-09T22:48:55.517 回答