几天前,我开始使用Phoenix 1.3,一切都很好,直到我开始在控制器中添加新动作。
Router.ex
resources "/users", UserController, only: [:show] do
resources "/events", EventController do
get "/:id", EventController, :join, as: :join
end
end
Event_controller.ex
def join(conn, %{"event_id" => id}, current_user) do
#some stuff..
conn
|> put_status(:ok)
|> put_flash(:info, "You just joined Event")
|> redirect(to: page_path(conn, :index, current_user))
end
然后在我的show模板中:/event/show.html.eex
<%= link "To list", to: user_event_path(@conn, :index, @event.user) %>
<%= if @current_user.id != @user.id do %>
<h1> <%= @user.name %> Event ! </h1>
<h3> <%= @event.title %></h3>
<p><%= @event.description %></p>
<p><%= @event.location %></p>
<p><%= @event.price %></p>
<%= link "Join", to: user_event_path(@conn, :join, @current_user.id,
@event.id), class: "btn btn-default btn-xs" %>
我尝试在使用Phoenix 1.2及其工作的其他项目中做类似的事情,但它的代码并不完全相同。
有人可以帮我理解为什么它不起作用......
对不起我的英语!
错误说:
No function clause for Eventoma.Web.Router.Helpers.user_event_path/4
and action :join. The following actions/clauses are supported:
user_event_path(conn_or_endpoint, :create, user_id, opts \\ [])
user_event_path(conn_or_endpoint, :delete, user_id, id, opts \\ [])
user_event_path(conn_or_endpoint, :edit, user_id, id, opts \\ [])
user_event_path(conn_or_endpoint, :index, user_id, opts \\ [])
user_event_path(conn_or_endpoint, :new, user_id, opts \\ [])
user_event_path(conn_or_endpoint, :show, user_id, id, opts \\ [])
user_event_path(conn_or_endpoint, :update, user_id, id, opts \\ [])
(phoenix) lib/phoenix/router/helpers.ex:299: Phoenix.Router.Helpers.raise_route_error/5
答案 0 :(得分:2)
您可以运行mix phx.routes
查看哪些路线可用。
如果您在自己提供的路由器上运行此功能,您将看到:
$ mix phx.routes
user_path GET /users/:id MyApp.Web.UserController :show
user_event_path GET /users/:user_id/events MyApp.Web.EventController :index
user_event_path GET /users/:user_id/events/:id/edit MyApp.Web.EventController :edit
user_event_path GET /users/:user_id/events/new MyApp.Web.EventController :new
user_event_path GET /users/:user_id/events/:id MyApp.Web.EventController :show
user_event_path POST /users/:user_id/events MyApp.Web.EventController :create
user_event_path PATCH /users/:user_id/events/:id MyApp.Web.EventController :update
PUT /users/:user_id/events/:id MyApp.Web.EventController :update
user_event_path DELETE /users/:user_id/events/:id MyApp.Web.EventController :delete
user_event_join_path GET /users/:user_id/events/:event_id/:id MyApp.Web.EventController :join
最下面一个是加入路线:
user_event_join_path GET /users/:user_id/events/:event_id/:id MyApp.Web.EventController :join
:user_id
,:event_id
和id
都在场。
你真正想要的是:
<%= link "Join", to: user_event_path(@conn, :join, @current_user.id, @event.id), class: "btn btn-default btn-xs" %>
是以下路由器:
resources "/users", UserController, only: [:show] do
resources "/events", EventController
get "/events/:id", EventController, :join
end
结果如下:
user_event_path GET /users/:user_id/events/:id MyApp.Web.EventController :join
顺便说一句,如果您对数据进行了任何更改(动词:join建议),那么您应该使用PUT请求(基于此答案https://stackoverflow.com/a/630475/219743)
答案 1 :(得分:-1)
因此,我明白了,正在做完全错误的东西! 由于Phoenix是修改我的conn的一个大插件,答案是这样的:
router.ex:
post "/events/:id", EventController, :join
然后带来event_controller.ex:
def join(conn, %{"id" => id}, current_user) do
conn
|> put_flash(:info, "You just joined Event")
|> redirect(to: page_path(conn, :index))
end
将从模板调用/template/show.html.exx:
<%= link "Join", to: user_event_path(@conn, :join,
@current_user, @event),
method: :join,
class: "btn btn-danger btn-xs" %>
所以我希望如果有人得到同样的错误并且无法弄清楚发生了什么, “醒来新......休闲白色康恩。”