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

在开始之前,还是要不厌其烦的说说,那些不必要写的你自己搞把,反正大把人玩过...
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现况,接下去如下:
[b]Model[/b]

class Photo < ActiveRecord::Base
acts_as_taggable
end

[b]Controller[/b]

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

[b]Helper[/b]

module ApplicationHelper
include TagsHelper
end


[b]View[/b]
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 %>

只作参考,请勿评论~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值