在行为-AS-taggable上的ActiveRecord ::关系问题 [英] ActiveRecord::Relation issue on acts-as-taggable-on

查看:231
本文介绍了在行为-AS-taggable上的ActiveRecord ::关系问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个轨道新手。我想实现的行为,作为-taggable,对我的示例应用程序。我能够使用tag_list,但面临的问题寻找他们进入多个标签。

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.

这是我得到了什么。

我用支架用户生成器和放大器;模型。

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

我也没有做任何改动类ActsAsTaggableOnMigration&LT; ActiveRecord的::安装宝石后迁移。

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

在我看来,我取代:标签有:tag_list在我_form,指数和放大器;显示HTML文件

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?

感谢你。

推荐答案

我猜(因为你没有提供的从其他视图中的code还没有),但: 当你这样做 @search = User.tagged_with(Tag11)是被退回的不是标签名,但实际的标签对象。如果你有:&LT;%= @search%&GT; 在你看来,这是行不通的。你需要是这样的:

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(', ') %>

或类似的。

这篇关于在行为-AS-taggable上的ActiveRecord ::关系问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆