Acts-as-Taggable-on无法正确打印出name列,而不是对象本身的整个信息散列。如何设置显示页面以分别打印模型的每个标签?我到目前为止的代码如下。
show.html.erb
<%= @user.user_profile.tags.each do |tag| %>
<%= tag.name %>
<% end %>
结果显示在显示页面
#<ActsAsTaggableOn::Tag::ActiveRecord_Associations_CollectionProxy:0x2b7534d0> games [#<ActsAsTaggableOn::Tag id: 1, name: "games", created_at: "2017-08-25 15:54:30", updated_at: "2017-08-25 15:54:30", taggings_count: 1>] 1
控制台输出
Rendering users/show.html.erb within layouts/application
UserProfile Load (8.0ms) SELECT "user_profiles".* FROM "user_profiles" WHERE "user_profiles"."user_id" = $1 LIMIT $2 [["user_id", 5], ["LIMIT", 1]]
ActsAsTaggableOn::Tag Load (15.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."context" = $3 [["taggable_id", 2], ["taggable_type", "UserProfile"], ["context", "tags"]]
ActsAsTaggableOn::Tagging Load (2.0ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 [["taggable_id", 2], ["taggable_type", "UserProfile"]]
ActsAsTaggableOn::Tag Load (25.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 2], ["taggable_type", "UserProfile"]]
user_profile.rb
class UserProfile < ApplicationRecord
acts_as_taggable
acts_as_taggable_on :abilities
end
答案 0 :(得分:1)
尝试
<% @user.user_profile.tags.each do |tag| %>
<%= tag.name %>
<% end %>
(没有&#39; =&#39;在第一行的&#39;&lt;%&#39;之后)