Omar Renteria
2010-Jun-17 22:32 UTC
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! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Omar Renteria
2010-Jun-17 22:35 UTC
Re: Why do rails calls a method when I''m calling a property?
I would also add that the trouble is in Show view/controller. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jun-17 23:36 UTC
Re: Why do rails calls a method when I''m calling a property?
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:NilClassThat''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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jun-17 23:40 UTC
Re: Why do rails calls a method when I''m calling a property?
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:NilClassThat''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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Taywin
2010-Jun-18 16:51 UTC
Re: Why do rails calls a method when I''m calling a property?
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 On Jun 17, 6:32 pm, Omar Renteria <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > > 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! > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Omar Renteria
2010-Jun-19 23:08 UTC
Re: Why do rails calls a method when I''m calling a property?
But it exists...in deed when I try the same param in rails console the object is found. Taywin wrote:> 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-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rory McKinley
2010-Jun-20 18:52 UTC
Re: Re: Why do rails calls a method when I''m calling a property?
On 20/06/2010 01:08, Omar Renteria wrote:> But it exists...in deed when I try the same param in rails console the > object is found.<snip> Are both cases (console and code running on server) running under the same environment? E.g. development vs production -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Agoofin
2010-Jun-20 19:20 UTC
Re: Why do rails calls a method when I''m calling a property?
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. On Jun 17, 6:32 pm, Omar Renteria <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > > 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! > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.