2012-08-08 6 views
18

레일스가 자동으로 추가하는 경로는 무엇입니까? Question_path, question_path 등을 자동으로 얻는 Question 리소스가 있다고 가정 해 봅시다. 그들이 해결 한 것과 볼 수있는 것을 어디서 볼 수 있습니까?동적 경로 헬퍼 레일

답변

34

이 섹션에서는 @photo는 모델 객체입니다 당신이

photo_path(@photo.id) 

를 작성할 수 http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use

Verb Path    Action  Helper 

GET  /photos   index  photos_path 
GET  /photos/new  new  new_photo_path 
POST /photos   create  photos_path 
GET  /photos/:id  show  photo_path(:id) 
GET  /photos/:id/edit edit  edit_photo_path(:id) 
PUT  /photos/:id  update  photo_path(:id) 
DELETE /photos/:id  destroy photo_path(:id) 

도움이 될 수 있습니다. 또는 id 메소드에 응답하는 경우 @photo을 직접 전달할 수 있습니다. 당신이에 다음과 같은 경우

photo_path(@photo) 
edit_photo_path(@photo) 

당신은 또한 (터미널)에서 rails console을로드 할 수 있습니다와 같은 app를 사용하여 시험 노선 그래서 app.photo_path(1)

8

그냥 사용이 정의 된 모든 경로를 나열합니다

rake routes 

. 첫 번째 열은 경로 도우미와 관련이 있습니다. 당신이 show 작업을위한 도우미를 만들려면

+0

[여기] (http://guides.rubyonrails.org/routing.html#seeing-existing-routes-with-rake) 자세한 설명입니다. '레이크 루트'가 제공하는 것 –

+0

어떻게 볼 수 없나요? 도우미 등에게 인수로서 전달할 수있는 것을 보여줍니다 ?? – LuckyLuke

+0

@Dude, 내 업데이트 참조 – evfwcqcg

0

(그것은 당신에게 id와 사진에 대한 경로 1 동일 표시됩니다) 귀하 루트 파일 :

resources :questions 

그런 레일은 당신을 위해 다음과 같은 편안한 경로를 제공

,
GET  /questions   index  list of questions 
GET  /questions/new  new   show new question form 
POST /questions   create  create a new question 
GET  /questions/:id  show  show a specific question 
GET  /questions/:id/edit edit  show form to edit question 
PUT  /questions/:id  update  update a specific question 
DELETE /questions/:id  destroy  delete a specific question 

rake : routes를 실행하여 생성되는 내용을 볼 수도 있습니다.