Works in Ruby but not in Rails

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@hotmail.com", created_at: "2010-02-25 10:52:32", updated_at: "2010-02-25 10:52:32">

Why doesn't this

<%=@comment.user %>

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:

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

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:

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

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

Shouldn't that be <%= @comment.user.name %> ?

period instead of a comma

Mikel

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

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

Sorry typo

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

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.