我有一些代碼是應該被覈查是否存在屬性的方法調用,它似乎既不是如果要不回來是什麼...如果respond_to?不返回任何東西
%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"
但是,如果我只是= comment.user.first_name
沒有條件語句,然後它工作正常..
或者你可以將這種方法添加到你的'User'模型'def名稱;用戶名||名字;結束「,那麼你可以在你的視圖上調用'user.name'。 – j03w