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 に答える