有什么方法可以在Phoenix中定义自定义路线? [英] Is there any way to define custom routes in Phoenix?

查看:83
本文介绍了有什么方法可以在Phoenix中定义自定义路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要创建一个resources并添加一些自定义操作,rails中的类似物是:

Let's say I want to create a resources with adding a couple of custom actions to it, the analogue in rails is:

resources :tasks do
  member do
    get :implement
  end
end

这不仅会给我返回7条标准路线,而且还会返回1条新路线:

Which will return me not only 7 standard routes, but 1 new:

GET /tasks/:id/implement

如何在凤凰城做到这一点?

How can I do it in phoenix?

推荐答案

我想稍微改善Dogbert的答案:

resources "/tasks", TaskController do
  get "/implement", TaskController, :implement, as: :implement
end

唯一的加法是路径末尾的as: :implement.

The only addition is as: :implement in the end of the route.

因此,您将获得名为task_implement_path的路由,而不是难看的task_task_path.

Thus you will get route named task_implement_path instead of ugly task_task_path.

这篇关于有什么方法可以在Phoenix中定义自定义路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆