asi*_*niy 17 elixir phoenix-framework
假设我想创建一个resources向它添加几个自定义动作,rails中的模拟是:
resources :tasks do
member do
get :implement
end
end
Run Code Online (Sandbox Code Playgroud)
这不仅会给我带来7条标准路线,还会带来1条新路线:
GET /tasks/:id/implement
Run Code Online (Sandbox Code Playgroud)
我怎么能在凤凰城做到这一点?
den*_*lin 23
我想Dogbert稍微改善一下答案:
resources "/tasks", TaskController do
get "/implement", TaskController, :implement, as: :implement
end
Run Code Online (Sandbox Code Playgroud)
唯一的补充是as: :implement在路线的尽头.
因此,您将获得命名task_implement_path而不是丑陋的路线task_task_path.
Dog*_*ert 16
你可以get在do块里面添加一个resources.
resources "/tasks", TaskController do
get "/implement", TaskController, :implement
end
Run Code Online (Sandbox Code Playgroud)
$ mix phoenix.routes
task_path GET /tasks MyApp.TaskController :index
task_path GET /tasks/:id/edit MyApp.TaskController :edit
task_path GET /tasks/new MyApp.TaskController :new
task_path GET /tasks/:id MyApp.TaskController :show
task_path POST /tasks MyApp.TaskController :create
task_path PATCH /tasks/:id MyApp.TaskController :update
PUT /tasks/:id MyApp.TaskController :update
task_path DELETE /tasks/:id MyApp.TaskController :delete
task_task_path GET /tasks/:task_id/implement MyApp.TaskController :implement
Run Code Online (Sandbox Code Playgroud)
Tsu*_*omu 11
这是另一个解决方案:
scope "/tasks" do
get "/:id/implement", TasksController, :implement
get "/done", TasksController, :done
end
resources "/tasks", TasksController
Run Code Online (Sandbox Code Playgroud)
该implement操作具有成员路由,并且该done操作具有收集路由.
您可以通过此函数调用获取前者的路径:
tasks_path(@conn, :implement, task)
Run Code Online (Sandbox Code Playgroud)
请注意,您应该在块之后放置该resources行.否则,凤凰城认为是行动的道路.scope/tasks/doneshow
| 归档时间: |
|
| 查看次数: |
4743 次 |
| 最近记录: |