2016-10-27 5 views
0

:id 매개 변수에서 route globbing을 사용하려면 별도의 : show route를 만들려고합니다.레일 5 단일 리소스 경로 이름 이름 바꾸기

resource :test, except: [:show] 
get 'test/*id', to: 'test#show', as: :test 

문제가 나는 오류가 발생한다는 것입니다 : You may have defined two routes with the same name using the : 나는 as: :test을 제거하면 option, or you may be overriding a route already defined by a resource with the same naming.

로 작동이, 나는 쇼 노선이없는 자원 경로와 별도의 쇼 경로를 만들었습니다. rails routes 쇼 :

tests POST /tests(.:format) 
new_test GET /tests/new(.:format) 
edit_test GET /tests/:id/edit(.:format) 
    test PATCH /tests/:id(.:format)  <-- WHY?? 
      DELETE /tests/:id(.:format) 
      GET /tests/*id(.:format) 

당신이 볼 수 있듯이, resources:testPATCH 경로를 이름. 해당 경로를 제거하면 test 등의 이름으로 DELETE 경로가 지정됩니다. resourcestest 경로 이름을 구체적으로 사용하는 것을 어떻게 중지시킬 수 있습니까? resource 블록 위의 globbing 경로를 분명히 이동시킬 수는 없습니다. 왜냐하면 다른 모든 경로도 globed되기 때문입니다.

tests POST /tests(.:format) 
new_test GET /tests/new(.:format) 
edit_test GET /tests/:id/edit(.:format) 
      PATCH /tests/:id(.:format) 
      DELETE /tests/:id(.:format) 
    test GET /tests/*id(.:format) 

답변

0

나는 당신의 문제를 이해하지 않는,하지만 당신은 레일 가이드 "단수 자원"에 보면 당신은 볼 수 있습니다 :

내가 원하는 것은

Path  Controller  #Action   Used for 
GET  /geocoder/new geocoders#new  return an HTML form for creating the geocoder 
POST  /geocoder  geocoders#create create the new geocoder 
GET  /geocoder  geocoders#show display the one and only geocoder resource 
GET  /geocoder/edit geocoders#edit return an HTML form for editing the geocoder 
PATCH/PUT /geocoder  geocoders#update update the one and only geocoder resource 
DELETE /geocoder  geocoders#destroy delete the geocoder resource 

쇼를 생성, 업데이트 파괴하지만 동일한 경로를 사용하지만 다른 HTTP 동사를 사용합니다. 그리고 귀하의 경우 test은 PATCH 동사로 작성했습니다.이 동사가 테이블의 앞부분에 있기 때문에 빈 이름은 상단 이름과 동일한 이름을 사용한다는 것을 의미합니다.

+0

다른 동사가있는 경로에서 동일한 경로 이름을 사용할 수 있다면 왜이 오류가 발생합니까? 'GET' 동사와 함께'test'라는 경로는 없습니다. –

+0

경로 이름이 여러 동사에 동시에 사용된다는 것을 모르기 때문에이 대답을 올바로 수락합니다. 이 방법으로, 나는 이미 test라는 이름으로 내 경로를': : test'라고 부를 필요가 없습니다. –

+0

@EikeCochu이 오류가 발생하면 무엇을하고 있습니까? 어떻게 사용하려고합니까? – piton4eg

1

Rails는 네 개의 경로 (show (GET), update (PUT/PATCH), destroy (DELETE))에 대해 동일한 접두사를 사용합니다 (예 : "test" HTTP 동사.

우선
+0

나는 본다. 나는 빈 경로 이름이 경로 이름이 아니라 이전 경로 이름을 의미한다고 생각했다. –

0

,

test PATCH /tests/:id(.:format)  <-- WHY?? 
     DELETE /tests/:id(.:format) 
     GET /tests/*id(.:format) 
  1. 패치는 업데이트 방법 경로입니다.
  2. 삭제는 삭제 방법 경로입니다. 그래서 get 'test/*id', to: 'test#show', as: :test

에 의해 생성 된 사용자 정의 경로로 추가

  • , 여기 당신은 다른 별명을 가진 쇼 경로를 만들 수 있습니다. 예를 사용하여 같이 as :show_test