gpt4 book ai didi

ruby-on-rails - 谁能想出一种在 Rails 中创建智能或动态路线的方法?

转载 作者:行者123 更新时间:2023-12-02 05:10:11 25 4
gpt4 key购买 nike

鉴于我有 Controller :

  app/controllers/app1/users_controller.rb  app/controllers/app2/users_controller.rb

我的路线文件中有:

["app1", "app2"].each do |n|  constraints(:host => "#{n}.com") do    scope({:as => vn, :module => vn}) do      resources :users    end  endend

这给了我这样的路线:

GET app1_users_path (app1.com/users) { :controller => "app1/users", :action => "index" }GET app2_users_path (app2.com/users) { :controller => "app2/users", :action => "index" }

我为应用程序中的每个路径、每个“应用程序”执行此操作。

问题是,没有。应用程序和路径的增长,因此没有。路径

n = no_of_pathsa = no_of_apps(n * a) = "LOADS"

谁能想到一种方法可以将路由的“模块”部分( Controller 前缀)设置为通配符,这样我只需为每个路由命名一次?

类似于:

match ":controller/:action(/:id)" => ":host/:controller#:action"

也许吧?

最佳答案

就是这样;它被称为包罗万象的路线。查看更多here .但是,您应该注意到,不到一周前,有很多关于包罗万象的路线的讨论,您可以继续阅读 here .

请注意下面的引用引用了 Rails 3.1 - 由 Ryan Bigg (aka Radar) 制作.

The catch-all route at the bottom of config/routes.rb is now commented out because having it there by default is not only unnecessary, but also really dangerous. If that link exists, then I would be able to give you an innocent looking URL (such as one provided by bit.ly) that linked to /admin/users/1/destroy, or whatever. Can you see the problem here? By having this commented out, only the routes that you've explicitly defined are going to be available. Most applications don't need the catch-all anyway.

关于ruby-on-rails - 谁能想出一种在 Rails 中创建智能或动态路线的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6410508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com