Rails 控制器动作路由
我希望根据控制器中定义的操作“自动”生成路由。资源路由(据我所知)仅自动生成路由和资源路由。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正常的 /: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.
你可以这样做:
也就是说,你可以使用上面的方法在控制器内对不同的路由进行分组。
You can do something like this:
That is, you can group different routes within a controller using the method above.