`
fireflyman
  • 浏览: 112847 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

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

    博客分类:
  • ROR
阅读更多
在开始之前,还是要不厌其烦的说说,那些不必要写的你自己搞把,反正大把人玩过...
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 %>

只作参考,请勿评论~
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics