【问题标题】:Why doesn't article.tag_list display the tags?为什么 article.tag_list 不显示标签?
【发布时间】:2015-03-22 10:07:09
【问题描述】:

我在 Gemfile 中添加了 gem 'acts-as-taggable-on' -v 2.3.1。 Rails 版本 3.2.13,Ruby -v 1.9.3

在article.rb中,

class Article < ActiveRecord::Base
  acts_as_taggable_on :tags
  has_many :comments, dependent: :destroy

  validates :title, presence: true,
                length: { minimum: 5 }
end

在articles_controller.rb中,

class ArticlesController < ApplicationController

before_filter :authenticate_author!, except: [:index, :show]

def index
  myarray = Article.all
  @articles = Kaminari.paginate_array(myarray).page(params[:page]).per(10)
end

def show
  @article = Article.find(params[:id])
end

def new
  @article = Article.new
end

def create
  @article = Article.new(article_params)

  if @article.save
    redirect_to @article
  else
    render 'new'
  end
end

def edit
  @article = Article.find(params[:id])
end

def update
  @article = Article.find(params[:id])

  if @article.update_attributes(article_params)
    redirect_to @article
  else
    render 'edit'
  end
end

def destroy
  @article = Article.find(params[:id])
  @article.destroy

  redirect_to articles_path
end

private

def article_params
  params.require(:article).permit(:title, :text, :tag_list => [])
end

end

在文章/_form.html.erb中

<div class="field">
  <%= f.label :tag_list, "Tags (separated by commas)" %><br />
  <%= f.text_field :tag_list %>
</div>

我在安装acts-on-taggable-gem 后运行了以下命令,

rails generate acts_as_taggable_on:migration
rake db:migrate

在文章/index.html.erb中

<% @articles.each do |article| %>
  <tr>
    <td><%= article.title %></td>
    <td><%= article.text %></td>
    <td><%= article.tag_list %></td>
    <td><%= link_to 'Show', article_path(article) %></td>
    <% if author_signed_in? %> 
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Destroy', article_path(article),
            method: :delete,
            data: { confirm: 'Are you sure?' } %></td>
    <% end%>
  </tr>
<% end %>

标签未显示在索引页面中。 但如果我这样做了,

article = Article.new(:title => "Awesome")
article.tag_list = "awesome, cool"
article.save

事务被提交,标签显示在浏览器上。

为什么标签没有被保存并显示在索引页面中?

【问题讨论】:

    标签: ruby-on-rails-4 ruby-on-rails-3.2 ruby-1.9.3 acts-as-taggable-on


    【解决方案1】:

    标签列表是一个数组,这意味着您需要“循环”来提取数据。每当我使用acts_as_taggable 时,我都会运行一个循环来提取标签。像下面这样的东西应该可以工作:

    <% @articles.each do |article| %>
      <tr>
        <td><%= article.title %></td>
        <td><%= article.text %></td>
    
        <td>
            <% article.tag_list.each do |tag| %>
                    <%= tag %>
            <% end %>
        </td>
    
        <td><%= link_to 'Show', article_path(article) %></td>
        <% if author_signed_in? %> 
          <td><%= link_to 'Edit', edit_article_path(article) %></td>
          <td><%= link_to 'Destroy', article_path(article),
                method: :delete,
                data: { confirm: 'Are you sure?' } %></td>
        <% end%>
      </tr>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      相关资源
      最近更新 更多