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
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
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.
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.
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.
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.