2016-11-13 7 views
0

호출되는 오류, 나는 다음과 같은 두 가지 경로를 만들어 "Enumerable에서 프로토콜이 구현되지"그들과 함께피닉스 방출 경로 도우미는 피닉스 앱

scope "/", Greeter do 
    pipe_through :browser 

    get "/hello", HelloController, :show 
    get "/hello/:name", HelloController, :show 
end 

을, 응용 프로그램이 응답 할 수있는 양 "/ 안녕하세요" 및 "/ hello/alice"경로가 있습니다.

그러나, 나는 "/ 인사/앨리스"를 생산하는 경로 도우미 hello_path(@conn, :show, "alice")를 사용할 때, 피닉스 서버가이 오류 메시지가 방출 :

프로토콜 Enumerable에서 "앨리스"

원인이 구현되지 않음 단순한.

첫 번째 경로는 두 개의 헬퍼 hello_path/2hello_path/3을 만들지 만 hello_path/3가 이미 정의되어 있기 때문에 두 번째 경로는 하나의 도우미 hello_path/4을 만듭니다.

hello_path/3을 열거 할 수 있으며 세 번째 인수로이 필요합니다.

이 오류를 방지하려면 어떻게해야합니까?

답변

1

당신은 다른 이름이 as:를 사용하여 경로 중 하나를 제공 할 수 있습니다 :

get "/hello", HelloController, :show 
get "/hello/:name", HelloController, :show, as: :hello_with_name 

테스트 :

iex(1)> import MyApp.Router.Helpers 
MyApp.Router.Helpers 
iex(2)> hello_path MyApp.Endpoint, :show 
"/hello" 
iex(3)> hello_path MyApp.Endpoint, :show, foo: "bar" 
"/hello?foo=bar" 
iex(4)> hello_with_name_path MyApp.Endpoint, :show, "alice" 
"/hello/alice" 
iex(5)> hello_with_name_path MyApp.Endpoint, :show, "alice", foo: "bar" 
"/hello/alice?foo=bar"