基于名称空间 Rails 3 覆盖公共文件夹

发布于 2024-10-22 01:34:00 字数 965 浏览 6 评论 0原文

经过几个小时的搜索后,我在 Stack Overflow 上发布了我的第一个问题。

给出:我有以下route.rb:

 resource: :non_namespaced_resource

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

期望结果:是让命名空间资源使用自己的资产,非命名空间资源使用默认资产,如下所示:

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

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

我已经看到有关更改 config/environments/*.rb 或 config/application.rb 的信息并使用类似以下内容的信息,但找不到任何基于此的示例在命名空间上。

请记住,只有两个命名空间 ADMIN 和 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

问题:看来这应该是可能的。所以我的问题是,这可能吗?如果是这样,怎么办?提前致谢。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2024-10-29 01:34:00

这比我习惯的要困难一些,但我希望它能有所帮助。

第一步是识别一些仅针对您的 admin 命名空间运行的代码。我可能会创建另一个应用程序控制器,也许是 admin_application_controller.rb,它从您的基本应用程序控制器扩展,然后从控制器扩展为您的所有管理控制器。例如。

# 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

我很想知道这是否适合您,如果不适合,您会遇到什么问题以及您发现了什么,所以请告诉我们!祝你好运!!

[编辑]我更新了上面的代码以反映可用的成员:

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"]}

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"]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文