ActiveRecord::Relation issue on acts-as-taggable-on

孤者浪人 提交于 2019-12-10 12:17:14

问题


I am a rails newbie. I am trying to implement acts-as-taggable-on on my sample app. I am able to enter multiple tags using tag_list but facing issues searching them.

This is what I got.

I used scaffold User to generate the controller & model.

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.text :tags

      t.timestamps
    end
  end
end

My User Model is

class User < ActiveRecord::Base
  serialize :tags
  acts_as_taggable_on :tags
  scope :by_join_date, order("created_at DESC")  
end

My User controller

Class UsersController < ApplicationController
def index
    @users = User.all
    @search = User.tagged_with("Tag11")
end
...
...
...
end

I also did not make any changes to class ActsAsTaggableOnMigration < ActiveRecord::Migration after installing the gem.

In my view I replaced :tags with :tag_list in my _form, index & show html files

<div class="field">
<%= f.label :tags %><br />
<%= f.text_field :tag_list %>
</div>

This is what I get in the browser

Could you please help me understand where I am making a mistake?

Thank you.


回答1:


I'm guessing (because you haven't provided the code from your other view yet) but: when you do @search = User.tagged_with("Tag11") what is getting returned is not the tag names, but the actual tag objects. If you have: <%= @search %> in your view, it won't work. You'll need something like:

<%= @search.map(&:name).join(', ') %>

or similar.



来源:https://stackoverflow.com/questions/8575153/activerecordrelation-issue-on-acts-as-taggable-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!