This works>> c=Comment.find(:first)=> #<Comment id: 1, story_id: 1, content: " Good", created_at: "2010-02-23 13:10:50", updated_at: "2010-02-23 13:19:19", user_id: 1>>> c.user_id=> 1>> c.user=> #<User id: 1, login: "gleb", password: "souhami", name: "Neil Bye", email: "neil_bye-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org", created_at: "2010-02-25 10:52:32", updated_at: "2010-02-25 10:52:32">>>Why doesn''t this <%=@comment.user %> -- 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.
That is because you are trying to diplay a comment object in the view, which is technically not possible. You might want to display the individual attributes of the @comment.user object, by calling <%= @comment.user.login %> Neil Bye wrote:> This works > >>> c=Comment.find(:first) > => #<Comment id: 1, story_id: 1, content: " Good", created_at: > "2010-02-23 13:10:50", updated_at: "2010-02-23 13:19:19", user_id: 1> >>> c.user_id > => 1 >>> c.user > => #<User id: 1, login: "gleb", password: "souhami", name: "Neil Bye", > email: "neil_bye-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org", created_at: "2010-02-25 10:52:32", > updated_at: "2010-02-25 10:52:32"> >>> > > Why doesn''t this > > <%=@comment.user %>-- 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.
Punit Rathore wrote:> That is because you are trying to diplay a comment object in the view, > which is technically not possible. You might want to display the > individual attributes of the @comment.user object, by calling <%= > @comment.user.login %> >>> >> Why doesn''t this >> >> <%=@comment.user %>Sorry should have been <%= @comment.user,name %> which still doesn''t work -- 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.
Are you sure you are finding the @comment correctly? What is the error you are getting?? Trying inspecting the @comment in the controller to see what is actually happening(by looking at the log). Type this line in the controller method where you are fetching the comment. @comment.inspect Neil Bye wrote:> > Sorry should have been <%= @comment.user,name %> which still doesn''t > work-- 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.
On 3 March 2010 22:53, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > Sorry should have been <%= @comment.user,name %> which still doesn''t > workNeil, You''ve got to give more information about what and how it "doesn''t work"; where are you placing that code (in a view, I assume... but I don''t *know*), what does the controller method look like, what error message are you getting (nomethod? unexpected kend?...) Please help us to help you, because as it stands, all I can say from what you''ve given is: it doesn''t work because something''s wrong. :-/ -- 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.
On Thu, Mar 4, 2010 at 7:42 PM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 3 March 2010 22:53, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> Sorry should have been <%= @comment.user,name %> which still doesn''t >> work >Shouldn''t that be <%= @comment.user.name %> ? period instead of a comma Mikel -- http://lindsaar.net/ -- 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.
> Neil, > > You''ve got to give more information about what and how it "doesn''t > work"; where are you placing that code (in a view, I assume... but I > don''t *know*), what does the controller method look like, what error > message are you getting (nomethod? unexpected kend?...) >This is the error: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.login Extracted source (around line #2): 1: <p id="story"><%= comment.content %></p> 2: <p>Submitted by:<%=comment.user.login %></p> The line occurs in a partial: <p id="story"><%= comment.content %></p> <p>Submitted by:<%=comment.user.login %> user.rb class User < ActiveRecord::Base has_many :stories has_many :comments has_many :stories_commented_on, :through => :comments, :source => :story def to_param "#{id}-#{login}" end end comment.rb class Comment < ActiveRecord::Base belongs_to :story belongs_to :user end story.rb class Story < ActiveRecord::Base after_create :create_initial_comment validates_presence_of :name, :link has_many :comments belongs_to :user def to_param "#{id}-#{name.gsub(/\W/, ''-'').downcase}" end protected def create_initial_comment comments.create :user => user end routes.rb map.resources :comments map.resources :stories map.resources :users map.resource :session map.resources :stories, :has_many => :comments map.resources :users, :has_many => :comments map.connect '':controller/:action/:id'' map.connect '':controller/:action/:id.:format'' comments_controller.rb class CommentsController < ApplicationController def create @story = Story.find(params[:story_id]) @comment = @story.comments.build(params[:comment]) @story.comment.save end def update @comment = Comment.all end def new @comment = Comment.new end end stories_controller.rb class StoriesController < ApplicationController def index @story = Story.all end def new @story = Story.new end def show @story = Story.find(params[:id]) end def update @story = Story.find(params[:id]) @comment = @story.comments.build(params[:comment]) @comment.save flash[:notice] = ''Comment submission succeeded'' redirect_to story_path(@story) end def create @story = @current_user.stories.build params[:story] @story.save flash[:notice] = ''Story submission succeeded'' redirect_to stories_path end protected def fetch_stories(conditions) @stories = Story.find :all, :order => ''id ASC'', :conditions => conditions end end users_controller class StoriesController < ApplicationController def index @story = Story.all end def new @story = Story.new end def show @story = Story.find(params[:id]) end def update @story = Story.find(params[:id]) @comment = @story.comments.build(params[:comment]) @comment.save flash[:notice] = ''Comment submission succeeded'' redirect_to story_path(@story) end def create @story = @current_user.stories.build params[:story] @story.save flash[:notice] = ''Story submission succeeded'' redirect_to stories_path end protected def fetch_stories(conditions) @stories = Story.find :all, :order => ''id ASC'', :conditions => conditions end end -- 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.
>>> >>> Sorry should have been �<%= @comment.user,name %> which still doesn''t >>> work >> > > Shouldn''t that be <%= @comment.user.name %> ? > > period instead of a comma > > MikelSorry typo -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Mar-04 10:42 UTC
Re: Re: Re: Works in Ruby but not in Rails More info
On 4 March 2010 10:29, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.login > > Extracted source (around line #2): > > 1: <p id="story"><%= comment.content %></p> > 2: <p>Submitted by:<%=comment.user.login %></p> >You''re being told that you have a nil object on line 2 when evaluating the "login" method. So "user" is nil - and I''d guess we''d expect it to be a User object... I can''t see anywhere in the controller (CommentController I assume) that assigns a user to a comment, so that''s a good place for you to start. Make sure somewhere does something along the lines of "comment.user current_user"... even hard-code comment.user_id to a known value to check that when there *is* a user on the comment, the view displays correctly. Then you can go back and make sure the comment is being built properly. Hope this helps. Michael -- 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.
On Thu, Mar 4, 2010 at 5:42 AM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 4 March 2010 10:29, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> You have a nil object when you didn''t expect it! >> The error occurred while evaluating nil.login >> >> Extracted source (around line #2): >> >> 1: <p id="story"><%= comment.content %></p> >> 2: <p>Submitted by:<%=comment.user.login %></p> >> > > You''re being told that you have a nil object on line 2 when evaluating > the "login" method. So "user" is nil - and I''d guess we''d expect it to > be a User object... > I can''t see anywhere in the controller (CommentController I assume) > that assigns a user to a comment, so that''s a good place for you to > start. > Make sure somewhere does something along the lines of "comment.user > current_user"... even hard-code comment.user_id to a known value to > check that when there *is* a user on the comment, the view displays > correctly. Then you can go back and make sure the comment is being > built properly.It might also be an ideal to add validates_presence_of :user to the comment model and either use save! or check the result of save when creating the comments. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- 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.