开发者

Overriding Public folder based on namespace Rails 3

After searching for hours, I am posting my first question on Stack Overflow.

Given: I have the following route.rb:

 resource: :non_namespaced_resource

 namespace :namespaced_resource do  # an example could be :admin
   resources :one_nested_resource
   resources :another_nested_resource
 end

Desired Outcome: Is to have namespaced resources use its own assets and non-namespaced resources use the default assets, as shown below:

# non-namespaced
/public
/public/images
/public/javascripts
/public/stylesheets

# namespaced
/admin
/admin/images
/admin/javascripts
/admin/stylesheets

I have seen information on changing config/environments/*.rb or config/application.rb and use something like the following, but can not find any examples to do this based on the namespace.

Keep in 开发者_JAVA技巧mind there will ONLY be two namespaces ADMIN and PUBLIC.

config.action_controller.asset_path
config.action_controller.asset_dir
config.action_controller.javascripts_dir
config.action_controller.stylesheets_dir
config.action_controller.images_dir

Question: It seems this should be possible. So my question(s) is, is this possible? If so, how? Thanks in advance.


This is going on a bit more of a limb than I'm used to, but I hope it helps.

The first step would be to identify some code that is run only for your admin namespace. I would probably create another application controller, perhaps admin_application_controller.rb, that extended from your base application controller, and then extend from that controller for all your admin controllers. For example.

# your basic applications controller
class ApplicationController < ActionController::Base
  protect_from_forgery
  # etc
end

# your public controllers subclass it
class UsersController < ApplicationController
  # stuff
end

# now your "specialized" admin controller
class AdminApplicationController < ApplicationController
  before_filter :setup_asset_paths

  def setup_asset_paths
    Rails.application.config.action_controller.assets_dir = File.expand_path(File.join(Rails.root, 'admin'))
    Rails.application.config.action_controller.javascripts_dir = File.expand_path(File.join(Rails.root, 'admin', 'javascripts'))
    Rails.application.config.action_controller.stylesheets_dir = File.expand_path(File.join(Rails.root, 'admin', 'stylesheets'))
    Rails.application.config.action_controller.page_cache_directory = File.expand_path(File.join(Rails.root, 'admin'))
  end
end

# and your admin controllers extend from THAT instead
class AdminUsersController < AdminApplicationController
  # more admin-y stuff
end

I'm quite interested to hear if this works for you, and if not, what problems you run into and what you find, so let us know! Good luck!!

[Edit] I've udpated the code above to reflect the members available:

pp Rails.application.config.action_controller
{:perform_caching=>false,
 :assets_dir=>"/Users/BinaryMuse/src/postecho/public",
 :javascripts_dir=>"/Users/BinaryMuse/src/postecho/public/javascripts",
 :stylesheets_dir=>"/Users/BinaryMuse/src/postecho/public/stylesheets",
 :page_cache_directory=>"/Users/BinaryMuse/src/postecho/public",
 :helpers_path=>["/Users/BinaryMuse/src/postecho/app/helpers"]}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜