【发布时间】: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