fireflyman 发表于 2013-2-8 01:06:53

快闪行动-->为你的项目添加标签模块

在开始之前,还是要不厌其烦的说说,那些不必要写的你自己搞把,反正大把人玩过...
1.安装插件-->
ruby script/plugin install git://github.com/jviney/acts_as_taggable_on_steroids.git

ruby script/generate acts_as_taggable_migration

rake db:migrate

-->这时你会有两个表:tags+taggings

2.针对我的TODO现况,接下去如下:
Model
class Photo < ActiveRecord::Base   acts_as_taggableend
Controller
class PhotosController < ApplicationControllerdef index    @photos = Photo.paginate(:page =>params[:page]||1,                           :per_page =>10,                           :order=>"updated_at DESC")   tag_cloud          respond_to do |format|      format.html # index.html.erb      format.xml { render :xml => @photos }    endend#tag method   def tag_cloud      @tags = Photo.tag_counts   end   def tag       @photos= Photo.find_tagged_with(params[:id])   end
Helper
module ApplicationHelperinclude TagsHelperend

View
1). 添加标签:在new.html.erb中,
<p>    <%= f.label :tags,'标签' %><br />    <%= f.text_field :tag_list,:size=>'60' %>*多个标签以","隔开</p>*注意: tag_list字段并不在 Item表中有,多个标签以","隔开
2). 显示标签,在index.html.erb中,
<div style="padding:20px">tags: <% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %>    <%= link_to tag.name, {:action => :tag, :id => tag.name }, :class => css_class %>   <% end %></div>
3). 显示标签详细:新建一个文件: photos/tag.erb
<% @photos.each do |photo| %><tr>    <td><%=h photo.title %></td></tr><% end %>
只作参考,请勿评论~
页: [1]
查看完整版本: 快闪行动-->为你的项目添加标签模块