Reputation: 11797
I have a bit of code that is supposed to be checking whether or not a method call exists for an attribute, it seems neither the if or else are returning anything...
%h2= @post.title
.row
.col-sm-12
= render "blog_meta", :post => @post
#text_body
= @post.content
%hr
- if @post.comments.count > 0
%h2 Comments
#comments
- @post.comments.where(:original_id => nil).each do |comment|
.comment
.profile
%img{:src => "/assets/profile_image_sample.jpg"}
.message
.username
- if comment.user.respond_to?('username')
.username= comment.user.username
- else
.username= comment.user.first_name
= comment.content
- logger.info "LOGGER--->>" + comment.user.first_name
.reply-link
%a{:href => "#"} Reply to Comment
- if comment.replies.count > 0
- nesting = 0
- comment.replies.each do |comment|
= render "comment", :comment => comment, :nesting => nesting
%hr
= render "submit_comment"
However if I simply = comment.user.first_name
with no conditionals then it works fine..
Upvotes: 0
Views: 128
Reputation: 8003
most likely the user
responds to username
but it is blank
or nil
. You might want to do this instead
- if comment.user.username.blank?
.username= comment.user.first_name
- else
.username= comment.user.username
respond_to
is used to check if a method is defined for the object. It will return true
if the method is defined for the object. It does not concern what the output of that method is.
Upvotes: 2