【问题标题】:Add search method in phoenix REST controller and how to write the route?在phoenix REST控制器中添加搜索方法以及如何编写路由?
【发布时间】:2016-05-13 11:30:46
【问题描述】:

Phoenix 框架生成的 RESTful 控制器很棒,我将在控制器中添加名为 search 的方法。问题是如何在route.ex中添加url来访问方法?现在 route.ex 看起来像

范围“/api”,RanksApi 做 pipe_through :api

scope "/v1", V1, as: :v1 do
  resources "/users", UserController, except: [:new, :edit]
end

结束

【问题讨论】:

    标签: routes elixir phoenix-framework


    【解决方案1】:

    我脑海中的快速答案是:

    范围 "/v1", V1, as: :v1 do 获取“/用户/搜索”,用户控制器,:搜索 资源“/users”,UserController,除了:[:new, :edit] 结尾

    get "/users/search", UserController, :search 必须在 users RESTful 路由之前

    【讨论】:

    • 是的,没错。如果在 RESTful 路由之后定义搜索路由,它将被忽略,/users/search 中的“搜索”将被解释为 ID。
    猜你喜欢
    • 2022-10-14
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    相关资源
    最近更新 更多