Why do rails calls a method when I'm calling a property?

Well, I'm starting on RoR, so I got a Book called simply
rails...Following the steps in it sometimes when I try to call a
property, the browser shows me the next exception:

undefined method `name' for nil:NilClass

I don't know why...here's my controller:
#Controller starts here
class StoriesController < ApplicationController
  before_filter :login_required, :only => [ :new, :create]

  def index
    get_stories 'votes_count >= 5'
  end

  def bin
    get_stories ''
    render :action => 'index'
  end

  protected
  def get_stories(conditions)
    @stories = Story.find :all, :order => 'id DESC', :conditions =>
conditions
  end

  def new
    @story = Story.new
  end

  def create
    #@story = Story.new(params[:story])
    @story = @current_user.stories.build params[:story]
    if @story.save
      flash[:notice] = "La historia a sido agregada correctamente!"
      redirect_to stories_path
    else
      render :action => 'new'
    end
  end

  def show
    @story = Story.find_by_id(params[:id])
  end
end
#controller ends here

Here's my view:
#View starts here
<h2>
   <%= @story.name %>
</h2>
<h3>
    <span id="vote_score">
  Shoveadas => <%= @story.votes.size %>
    </span>
</h3>
<p class="submited_by"> Agregada por: <%= @story.user.login %> </p>
<p>
    <%= link_to @story.link, @story.link%>
</p>
<div id="vote_form">
  <% form_remote_tag :url => story_votes_path(@story) do %>
    <%= submit_tag 'Shovealo!' %>
  <% end %>
</div>
<ul id="vote_history">
  <% if @story.votes.empty? %>
    <em>Sin shoveadas aun</em>
  <% else %>
    <%= render :partial => 'votes/vote', :collection => @story.votes %>
  <% end %>
</ul>
#view ends here

From now thank you!

I would also add that the trouble is in Show view/controller.

Omar Renteria wrote:

Well, I'm starting on RoR, so I got a Book called simply
rails...Following the steps in it sometimes when I try to call a
property, the browser shows me the next exception:

undefined method `name' for nil:NilClass

That's how Ruby works: all data access is done through method calls.
Unlike in Java or C++, there is no way (well, except for tricky hacks)
to see an instance variable from outside the object that contains it.
This is one of the best features of Ruby, since it means calling
routines don't have to know whether they're asking for an instance
variable or a computation.

Best,

Omar Renteria wrote:

Well, I'm starting on RoR, so I got a Book called simply
rails...Following the steps in it sometimes when I try to call a
property, the browser shows me the next exception:

undefined method `name' for nil:NilClass

That's how Ruby works: all data access is done through method calls.
Unlike in Java or C++, there is no way (well, except for tricky hacks)
to see an instance variable from outside the object that contains it.
This is one of the best features of Ruby, since it means calling
routines don't have to know whether they're asking for an instance
variable or a computation.

Best,

If you look at the error "undefined method `name' for nil:NilClass,"
it tells you that the error is from attempting to call a method 'name'
from nil (NULL) object. I believe Rails would attempt to look for both
method and property; however, it would report as undefined 'method' if
it found nil object.

In your controller, you are using @story =
Story.find_by_id(params[:id]) to search for a story. When a story with
the incoming ID is not found, it returns and assigns nil to your
@story. In your view, you attempt to display 'name' but it is being
called from a nil object. If you want to prevent from error, check for
nil (and may take a proper action) before your let Rails render the
action.

Regards,
Taywin

But it exists...in deed when I try the same param in rails console the
object is found.

Taywin wrote:

<snip>

Are both cases (console and code running on server) running under the same environment? E.g. development vs production

Look at your link_to in the index.html, it should contain the id for
the story you want to show.

Also, check to make sure that you have the to_param method in your
model, story.db and that it's correct. The author implements a
complicated beautification of the urls which is a waste in the course
of the examples.

I have the same book, one of the best for learning rails imho.

One other question for you - there are some extra methods that are a
bit confusing, your bin method and the get_stories for example. I
don't think the saving of a few lines of code is worth the possible
confusion.