Rails 控制器动作路由

发布于 2024-12-10 18:34:33 字数 612 浏览 0 评论 0原文

我希望根据控制器中定义的操作“自动”生成路由。资源路由(据我所知)仅自动生成路由和资源路由。 http 动词的帮助器。在这种情况下,我主要使用 Rails 提供静态页面,并且在我的控制器中不需要 http 动词。

具体来说:

在控制器中,我定义了引用那些大部分静态页面的操作。

def action
end

在路由文件中,我有一堆

match "/url" => 'controller#action'

我希望根据控制器中的操作自动生成所有这些匹配的路由。概念上类似于:

for actions in controller.erb do |action|
    'match "/action" => "controller#action"
end

这可能吗?我可以直接将代码写在routes文件中吗?

我还需要考虑一些嵌套操作...控制器操作可能是:

def action
    def nested_action
    end
end

我很感激对此事的任何想法。谢谢。

I'm looking to "automatically" generate routes based on actions defined in a controller. resources routing (as far as I know) only automatically generates routes & helpers for http verbs. In this instance I'm serving mostly static pages using rails and have no need for http verbs in my controller.

Specifically:

In a controller I have defined actions which refer to those mostly static pages.

def action
end

In the routes file I have a bunch of

match "/url" => 'controller#action'

I'd like all those matched routes to be generated automatically based on the actions in the controller. Something CONCEPTUALLY along the lines of:

for actions in controller.erb do |action|
    'match "/action" => "controller#action"
end

Is this possible? Would I write the code in the routes file directly?

I also have some nested actions to consider... a controller action may be:

def action
    def nested_action
    end
end

I'd appreciate any thoughts on this matter. Thanks.

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

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

发布评论

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

评论(2

甜妞爱困 2024-12-17 18:34:33

正常的 /:controller/:action 想法有什么问题?

不处理嵌套操作,但是......我很难理解为什么你会想要这样。

What's wrong with the normal /:controller/:action idea?

That won't deal with nested actions, but... I'm having a difficult time understanding why you'd ever want that.

巷雨优美回忆 2024-12-17 18:34:33

你可以这样做:

controller :controller_name do
   get "path/action" => :method_name, :as => :path_action
   post "path/save" => :method_name, :as => :path_save
end

也就是说,你可以使用上面的方法在控制器内对不同的路由进行分组。

You can do something like this:

controller :controller_name do
   get "path/action" => :method_name, :as => :path_action
   post "path/save" => :method_name, :as => :path_save
end

That is, you can group different routes within a controller using the method above.

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