#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    8
    Rep 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    8
    Rep Power
    0
    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    8
    Rep Power
    0
    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?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    8
    Rep Power
    0
    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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    As I said, the problem is that your commentator is nil. So you should investigate why.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    8
    Rep Power
    0
    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
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    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.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    8
    Rep Power
    0
    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!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    thanks


    good frnd thanks

IMN logo majestic logo threadwatch logo seochat tools logo