在评论返回问题:: ActiveRecord_Associations_Collection [英] Returning issue in Comment::ActiveRecord_Associations_Collection
问题描述
我想显示所有来自用户在用户个人资料页面提出的意见(不管它是公共或登录后面的页面),但我遇到打印在我的应用程序注释的问题: :ActiveRecord_Associations_CollectionProxy:0x00000103c89750(这不是杀死我的应用程序错误,只是打印该消息)
I'm trying to display all the comments made from a user in a User profile page (no matter if it's a public or a page behind the login), but I'm experiencing an issue that prints in my app Comment::ActiveRecord_Associations_CollectionProxy:0x00000103c89750 (it's not an error that kills my app, just prints that message).
在我的应用程序的唯一元素commentable是黑客,并且用户可以创建每个黑客意见。
the only element commentable within my app are 'hacks', and users can create comments on each hack.
user.rb
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
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
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>
我用征求意见的宝石The_Comments,有的一些文档在这里,我还要广泛阅读,我认为的 @ user.comcoms 应该回到我在寻找什么,而不是:/
The gem I'm using for comments is The_Comments and there are some docs here that I have read widely, and I think @user.comcoms should return what I'm looking for but not :/
推荐答案
@ user.comcoms
将给予相关的特定用户的所有评论( @user
你的情况)commentable模型。因此,它会返回一个意见收集和评论:: ActiveRecord_Associations_CollectionProxy:0x00000103c89750
是显示该集合的参考
@user.comcoms
will give all comments related to the particular users(@user
in your case) commentable models. So, its going to return a collection of comments and Comment::ActiveRecord_Associations_CollectionProxy:0x00000103c89750
is showing the reference of that collection.
您需要遍历集合并从注释实例显示所需要的领域
类。
You need to iterate over the collection and display the required fields from an instance of Comment
class.
替换
<%= @user.comcoms %>
通过
<% @user.comcoms.each do |comment| %>
<%= comment.raw_content %>
<%= comment.commentable_title %>
<%# Add other attributes %>
<% end %>
请参阅您在问题补充的联系,对于可以在注释
的评论API
这篇关于在评论返回问题:: ActiveRecord_Associations_Collection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!