`
hellosoft
  • 浏览: 54824 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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
   编写内容
BODY {
   
   background-color:#00FF00;
}

同样创建 mytheme2/stylesheets/theme.css,内容相似,但是不一样:
 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
 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics