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 B.”,
email: “[email protected]”, created_at: “2010-02-25 10:52:32”,
updated_at: “2010-02-25 10:52:32”>

Why doesn’t this

<%[email protected] %>

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 B. 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 B.”,
email: “[email protected]”, created_at: “2010-02-25 10:52:32”,
updated_at: “2010-02-25 10:52:32”>

Why doesn’t this

<%[email protected] %>

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

<%[email protected] %>

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 B. wrote:

Sorry should have been <%= @comment.user,name %> which still doesn’t
work

On 3 March 2010 22:53, Neil B. [email protected] wrote:

Sorry should have been <%= @comment.user,name %> which still doesn’t
work

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

On Thu, Mar 4, 2010 at 7:42 PM, Michael P. [email protected]
wrote:

On 3 March 2010 22:53, Neil B. [email protected] 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/

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

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:

<%= comment.content %>


2:

Submitted by:<%=comment.user.login %>

The line occurs in a partial:

<%= comment.content %>

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

On 4 March 2010 10:29, Neil B. [email protected] 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:

<%= comment.content %>


2:

Submitted by:<%=comment.user.login %>

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

On Thu, Mar 4, 2010 at 5:42 AM, Michael P. [email protected]
wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs