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

在开始之前,还是要不厌其烦的说说,那些不必要写的你自己搞把,反正大把人玩过...
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_taggable
end

Controller
class PhotosController < ApplicationController


def 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 }
    end
end

#tag method
   def tag_cloud
      @tags = Photo.tag_counts
   end

   def tag
       @photos= Photo.find_tagged_with(params[:id])
   end

Helper
module ApplicationHelper
  include TagsHelper
end


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 %>

只作参考,请勿评论~

你可能感兴趣的:(xml,css,git,Ruby,ActiveRecord)