0

I have this controller:

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

It gets called when this link gets pressed:

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

so I am assuming that the get-all part of the controller would get invoked, and I commented out lines in the controller that I thought would get invoked, but I still get this error:

undefined method `each' for nil:NilClass

from line 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>

Any idea where the problem is happening?

Thanks!

1
  • @fl00r I thought commenting that line would stop the application from making a db call since I didn't make a schema yet. Commented Apr 9, 2012 at 22:48

1 Answer 1

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

=>

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

or

<%= link_to "Alex Link", @alex %>
Sign up to request clarification or add additional context in comments.

7 Comments

thanks. I am still confused about the differences of each of those syntaxes. Whats the difference between them? Thanks again!
It is convention. When you need something singular (one Alex) you should write singular routes, and if you want to get a bunch of items you should use pluralized routes
thanks! and the 3rd line..what implication does it have if you don't use the alex_path() ?
Also, how would it effect the error I am encountering? Where is that each loop? I dont see it in the controller.
It's in your view. @alexes.each do |alex|. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.