The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Ruby Programming
|
Commenting on an image
Discuss Commenting on an image in the Ruby Programming forum on Dev Shed. Commenting on an image Ruby and Ruby on Rails programming forum covering Ruby Tips and Tricks, Best Practices, and agile development with Ruby on Rails.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 3rd, 2012, 09:31 AM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 8
Time spent in forums: 1 h 54 m 52 sec
Reputation Power: 0
|
|
|
Commenting on an image
So I am trying to comment on an image and I am slightly confused on how
to do this. I am very new to Ruby so I don't know what to display for
you guys but I will show you.
I just need the method as I can't figure that out.
This is what I am trying to do.
shared/_comment.html.erb uses a method display_name. The object
comment.commentator will be an instance of Reader which will need to be
modified to
have this method, which will be their first name and last name
concatenated together.
I want to be able to comment on an image and for it to display their
name.
Thanks in advance
|

May 3rd, 2012, 11:02 AM
|
|
|
Your description is a bit vague, but it sounds like all you need is to go into your reader model and add the following method:
Code:
def display_name
first_name + last_name
end
If that's not what you wanted, please describe your problem further.
|

May 3rd, 2012, 12:39 PM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 8
Time spent in forums: 1 h 54 m 52 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by sepp2k1 Your description is a bit vague, but it sounds like all you need is to go into your reader model and add the following method:
Code:
def display_name
first_name + last_name
end
If that's not what you wanted, please describe your problem further. |
My apologies, I am really new to this and there was no way for me to upload the files to show.
|

May 4th, 2012, 08:07 AM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 8
Time spent in forums: 1 h 54 m 52 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by sepp2k1 Your description is a bit vague, but it sounds like all you need is to go into your reader model and add the following method:
Code:
def display_name
first_name + last_name
end
If that's not what you wanted, please describe your problem further. |
Hi, thanks for your help but I am now receiving this error.
private method `display_name' called for nil:NilClass
Is there any way to post the files?
|

May 4th, 2012, 10:40 AM
|
|
|
You can just copy and paste the relevant code into your post and add code-tags around it.
Anyway the error message means that the object you tried to call display_name on is nil. Assuming that you did something like "comment.reader.display_name" that means that the comment doesn't have a reader associated with it.
If that's allowed to happen (because you allow anonymous comments), you need to handle the case where reader is nil. One way to do that would be to add a reader_name method like the following to your comment model:
Code:
def reader_name
if reader
reader.display_name
else
"Anonymous"
end
end
And then only call that method in your view. If a comment should always have a reader associated with it, you need to find out why this one didn't. First thing I'd check is whether the database entry contains a reader_id and whether that id is correct. If that's not the case something went wrong when creating or saving the comment In that case you should also consider adding a validation that prevents comments from being created without a valid reader.
|

May 4th, 2012, 01:02 PM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 8
Time spent in forums: 1 h 54 m 52 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by sepp2k1 You can just copy and paste the relevant code into your post and add code-tags around it.
Anyway the error message means that the object you tried to call display_name on is nil. Assuming that you did something like "comment.reader.display_name" that means that the comment doesn't have a reader associated with it.
If that's allowed to happen (because you allow anonymous comments), you need to handle the case where reader is nil. One way to do that would be to add a reader_name method like the following to your comment model:
Code:
def reader_name
if reader
reader.display_name
else
"Anonymous"
end
end
And then only call that method in your view. If a comment should always have a reader associated with it, you need to find out why this one didn't. First thing I'd check is whether the database entry contains a reader_id and whether that id is correct. If that's not the case something went wrong when creating or saving the comment In that case you should also consider adding a validation that prevents comments from being created without a valid reader. |
Don't know if this is any use to you but this is my friends model.
class Friends
Code:
class Friends < ActiveRecord::Base
has_many :comments, :as => :commentator, :class_name =>"Commentable"
def display_name
"#{self.firstname} #{self.surname}"
end
|

May 4th, 2012, 01:18 PM
|
|
|
|
As I said, the problem is that your commentator is nil. So you should investigate why.
|

May 4th, 2012, 01:35 PM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 8
Time spent in forums: 1 h 54 m 52 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by sepp2k1 As I said, the problem is that your commentator is nil. So you should investigate why. |
My apologies, that problem was fixed. I am now receiving this problem.
undefined method `firstname' for #
friend.rb model
Code:
class Friends < ActiveRecord::Base
has_many :comments, :as => :commentator, :class_name =>"Commentable"
def display_name
"#{self.firstname} #{self.surname}"
end
end
artist model
Code:
class Artist < ActiveRecord::Base
has_many :publishings
has_many :images, :through => :publishings
has_many :comments, :as => :resource, :class_name => "Commentable"
end
artist show.html.erb
Code:
<p id="notice"><%= notice %></p>
<div id="container">
<p>
<b>First name:</b>
<%= @artist.firstname %>
</p>
<p>
<b>Second name:</b>
<%= @artist.surname %>
</p>
<p>
<b>about:</b>
<%= @artist.about %>
</p>
<div id="comments">
<h2>Comments</h2>
<%= render :partial => "shared/comment", :collection => @artist.comments%>
</div
</div>
<%= render :partial => "image", :collection => @artist.images %>
<%= link_to 'Edit', edit_artist_path(@artist) %> |
<%= link_to 'Back', artists_path %>
<%= link_to 'show', images_path %>
Comment Partial
Code:
<div class="comment">
<p>
<span class="commentator"><%= comment.commentator.display_name %>
say's</span>
<%= comment.comment %>
</p>
</div
|

May 4th, 2012, 02:32 PM
|
|
|
Quote: | Originally Posted by drumlegend My apologies, that problem was fixed. I am now receiving this problem.
undefined method `firstname' for # |
An important part of that error message was cut off because the forum software thought it was an HTML-tag. Please repost it in CODE-tags. It would also be interesting to see the stack trace. I.e. is the error raised from inside display_name or when you call firstname directly in the view.
|

May 5th, 2012, 11:09 AM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 8
Time spent in forums: 1 h 54 m 52 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by sepp2k1 An important part of that error message was cut off because the forum software thought it was an HTML-tag. Please repost it in CODE-tags. It would also be interesting to see the stack trace. I.e. is the error raised from inside display_name or when you call firstname directly in the view. |
I have resolved the issue, thanks for your help!
|

May 29th, 2012, 06:24 AM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 3
Time spent in forums: 30 m 53 sec
Reputation Power: 0
|
|
|
thanks
good frnd thanks
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|