Ruby on rails 在Comment::ActiveRecord\u Associations\u集合中返回问题
我正在尝试在用户配置文件页面中显示用户的所有评论,无论是公开的还是登录后的页面,但我遇到了一个问题,该问题会在我的应用程序Comment::ActiveRecord\u Associations\u CollectionProxy:0x0000103C89750中打印。这不是导致我的应用程序失败的错误,只是打印该消息 我的应用程序中唯一可以评论的元素是“黑客”,用户可以在每次黑客攻击中创建评论 user.rbRuby on rails 在Comment::ActiveRecord\u Associations\u集合中返回问题,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我正在尝试在用户配置文件页面中显示用户的所有评论,无论是公开的还是登录后的页面,但我遇到了一个问题,该问题会在我的应用程序Comment::ActiveRecord\u Associations\u CollectionProxy:0x0000103C89750中打印。这不是导致我的应用程序失败的错误,只是打印该消息 我的应用程序中唯一可以评论的元素是“黑客”,用户可以在每次黑客攻击中创建评论 user.rb class User < ActiveRecord::Base
class User < ActiveRecord::Base
TEMP_EMAIL_PREFIX = 'change@me'
TEMP_EMAIL_REGEX = /\Achange@me/
include Gravtastic
gravtastic
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :confirmable
validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update
def self.find_for_oauth(auth, signed_in_resource = nil)
# Get the identity and user if they exist
identity = Identity.find_for_oauth(auth)
# If a signed_in_resource is provided it always overrides the existing user
# to prevent the identity being locked with accidentally created accounts.
# Note that this may leave zombie accounts (with no associated identity) which
# can be cleaned up at a later date.
user = signed_in_resource ? signed_in_resource : identity.user
# Create the user if needed
if user.nil?
# Get the existing user by email if the provider gives us a verified email.
# If no verified email was provided we assign a temporary email and ask the
# user to verify it on the next step via UsersController.finish_signup
email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
email = auth.info.email if email_is_verified
user = User.where(:email => email).first if email
# Create the user if it's a new registration
if user.nil?
user = User.new(
name: auth.extra.raw_info.name,
#username: auth.info.nickname || auth.uid,
email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
password: Devise.friendly_token[0,20]
)
user.skip_confirmation!
user.save!
end
end
# Associate the identity with the user if needed
if identity.user != user
identity.user = user
identity.save!
end
user
end
def email_verified?
self.email && self.email !~ TEMP_EMAIL_REGEX
end
include TheComments::User
has_many :hacks
has_many :comments
def admin?
self == User.first
end
def comments_admin?
admin?
end
def comments_moderator? comment
id == comment.holder_id
end
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
include TheComments::Comment
# ---------------------------------------------------
# Define comment's avatar url
# Usually we use Comment#user (owner of comment) to define avatar
# @blog.comments.includes(:user) <= use includes(:user) to decrease queries count
# comment#user.avatar_url
# ---------------------------------------------------
# public
# ---------------------------------------------------
# Simple way to define avatar url
#
# def avatar_url
# src = id.to_s
# src = title unless title.blank?
# src = contacts if !contacts.blank? && /@/ =~ contacts
# hash = Digest::MD5.hexdigest(src)
# "https://2.gravatar.com/avatar/#{hash}?s=42&d=https://identicons.github.com/#{hash}.png"
# end
# ---------------------------------------------------
# private
# ---------------------------------------------------
# Define your content filters
# gem 'RedCloth'
# gem 'sanitize'
# gem 'MySmilesProcessor'
#
# def prepare_content
# text = self.raw_content
# text = RedCloth.new(text).to_html
# text = MySmilesProcessor.new(text)
# text = Sanitize.clean(text, Sanitize::Config::RELAXED)
# self.content = text
# end
# ---------------------------------------------------
end
hack.rb
class Hack < ActiveRecord::Base
belongs_to :user
acts_as_taggable # Alias for acts_as_taggable_on :tags
acts_as_taggable_on :tags
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
#For commenting
include TheComments::Commentable
# Denormalization methods
# Please, read about advanced using
def commentable_title
"Undefined Post Title"
end
def commentable_url
"#"
end
def commentable_state
"published"
end
end
用户视图
<p>
<strong>User email?:</strong>
<%= @user.email %>
<%= @user.comcoms %>
</p>
我用于评论的gem是_评论,这里有我已经广泛阅读的内容,我认为应该返回我正在寻找的内容,但不是:/@user.com将给出与特定内容相关的所有评论users@user在您的例子中,可注释模型。因此,它将返回一个注释集合,Comment::ActiveRecord_Associations_CollectionProxy:0x0000103C89750显示该集合的引用
您需要迭代集合并显示Comment类实例中的必需字段
替换
<%= @user.comcoms %>
与
请参阅您在问题中添加的链接,以了解可在注释中访问的属性
<% @user.comcoms.each do |comment| %>
<%= comment.raw_content %>
<%= comment.commentable_title %>
<%# Add other attributes %>
<% end %>