Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 3rd, 2012, 09:31 AM
drumlegend drumlegend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 8 drumlegend User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old May 3rd, 2012, 11:02 AM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 20 m 52 sec
Reputation Power: 38
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.

Reply With Quote
  #3  
Old May 3rd, 2012, 12:39 PM
drumlegend drumlegend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 8 drumlegend User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old May 4th, 2012, 08:07 AM
drumlegend drumlegend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 8 drumlegend User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old May 4th, 2012, 10:40 AM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 20 m 52 sec
Reputation Power: 38
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.

Reply With Quote
  #6  
Old May 4th, 2012, 01:02 PM
drumlegend drumlegend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 8 drumlegend User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 

Reply With Quote
  #7  
Old May 4th, 2012, 01:18 PM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 20 m 52 sec
Reputation Power: 38
As I said, the problem is that your commentator is nil. So you should investigate why.

Reply With Quote
  #8  
Old May 4th, 2012, 01:35 PM
drumlegend drumlegend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 8 drumlegend User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 

Reply With Quote
  #9  
Old May 4th, 2012, 02:32 PM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 20 m 52 sec
Reputation Power: 38
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.

Reply With Quote
  #10  
Old May 5th, 2012, 11:09 AM
drumlegend drumlegend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 8 drumlegend User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #11  
Old May 29th, 2012, 06:24 AM
fizagul44 fizagul44 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 3 fizagul44 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 53 sec
Reputation Power: 0
thanks

good frnd thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Commenting on an image

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap