是否可以动态更改使用控制器的路径?Ryan Bates 在这里展示了如何更改 view_paths:http ://railscasts.com/episodes/269-template-inheritance
我正在制作一个 CMS,用户可以在其中创建一个站点并输入他们自己的子域。如果没有子域,我希望“/”指向“public#welcome”,但如果有子域,我希望它指向“sites/public#welcome”。
如果这有什么不同,我正在使用 Rails 3.1。
是否可以动态更改使用控制器的路径?Ryan Bates 在这里展示了如何更改 view_paths:http ://railscasts.com/episodes/269-template-inheritance
我正在制作一个 CMS,用户可以在其中创建一个站点并输入他们自己的子域。如果没有子域,我希望“/”指向“public#welcome”,但如果有子域,我希望它指向“sites/public#welcome”。
如果这有什么不同,我正在使用 Rails 3.1。
You should be able to solve this situation using constraints if I'm not mistaken (which I might, since I haven't actually tried the following yet):
constraints(:subdomain => /.+/) do
root :to => 'sites/public#welcome'
end
root :to => 'public#welcome'
我想到了:
constraints(:subdomain => /.+/) do
scope :module => "sites" do
root :to => 'public#welcome'
end
end
root :to => 'public#welcome'
现在,当用户访问“/”时,如果存在子域,将使用 Sites::PublicController,但如果没有子域存在,则仅使用 PublicController。添加scope :module => "sites" do...end
使我的路线文件简单且易于管理。