rails plugin : theme

这个插件使用起来比较麻烦,
有耐心的朋友可以看看这个文章。
http://www.mattmccray.com/pivot/archive.php?c=Theme_Support

笔者摸索的半天也没有弄出个太多的东西,只是对背景颜色做了一些改动。
步骤如下:
1. 安装 theme
2. 更改theme中的routeset_ex文件,否则启动都成了问题。
vendor/plugins/theme_support/lib/patches/routeset_ex.rb
# Extends <tt>ActionController::Routing::RouteSet</tt> to automatically add the theme routes
class ActionController::Routing::RouteSet

alias_method :__draw, :draw

# Overrides the default RouteSet#draw to automatically
# include the routes needed by the ThemeController
def draw
clear!
map = Mapper.new(self)

create_theme_routes(map)
yield map

named_routes.install
end


# Creates the required routes for the ThemeController...
def create_theme_routes(map)
map.theme_images "/themes/:theme/images/*filename", :controller => 'theme', :action => 'images'
map.theme_stylesheets "/themes/:theme/stylesheets/*filename", :controller => 'theme', :action => 'stylesheets'
map.theme_javascript "/themes/:theme/javascript/*filename", :controller => 'theme', :action => 'javascript'
map.connect "/themes/*whatever", :controller => 'theme', :action => 'error'
end

end

3. 然后是 运行 gem install theme_generator,安装theme生成器
4. 生成theme :
#> ruby script/generate theme mytheme
mytheme 是 theme 名称,可以更改,比如: default, admin_theme ....
这样就可以在 rails应用 的根路径下生成一个文件夹 themes
最好生成两个,这样有一个比较
#> ruby script/generate theme mytheme2
将它移动到 public下面。
5.我们可以在themes下发现 有mytheme,mytheme2 目录
找到mytheme/stylesheets,并且在下面建立一个theme.css
编写内容
   
background-color:#00FF00;
}[/code]
同样创建 mytheme2/stylesheets/theme.css,内容相似,但是不一样:
[code=""> BODY {

background-color:#FFFF00;
}


好了,下面就是更改我们之前代码的问题了。

6. 在app/controller/application.rb中,
class ApplicationController < ActionController::Base
theme :get_user_theme

def get_user_theme
# This assumes, of course, your User has a 'theme' field
#return @session[:user].nil? ? 'default' : @session[:user].theme
return session[:username]=="admin" ? "mytheme" : 'mytheme2'
end
... ...
end


接着在我们的views页面中增加代码
e.g. app/views/layouts/homepage.rhtml
<html>
<head>
...
<link href="<%= theme_stylesheet_path %>" media="screen" rel="Stylesheet" type="text/css" />
...
</head>
...
</html>

好了,全部都设定好了,重新启动一下服务,然后使用不同的用户登录,admin登录的时候的homepage的背景颜色应该会与其他用户登录时候的不一样。
笔者 才疏学浅 ,就暂时研究到这里了。剩下的就看读者们自己继续研究了 。 good luck :)


参考文章:
http://www.mattmccray.com/pivot/archive.php?c=Theme_Support
http://www.webdrivenblog.com/2007/11/27/installing-the-theme-support-plugin-with-rails-1-2
http://agilewebdevelopment.com/plugins/theme_support
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值