2010-02-01 7 views
1

내가 루비 온 레일에 초보자이야 루비 온 레일즈에 경로를 생성하고 난 다음 일을하려고 마지막 시간을 보냈다 :라우팅

내가 루비 온 레일 응용 프로그램이이 - 블로그 게시물 및 카테고리가 포함되어 있습니다. 게시물에 대한 다른 URL을 갖고 싶습니다. http://localhost:3000/posts 대신 에서 News으로 컨트롤러와 클래스를 대체하려고 시도했지만, (단점 복수형 때문에) 포기했습니다. 내가 some websites에서 본 그런 다음 내에서 나는 routes.rb

map.resources :news, :controller => "posts"  #case 2 

또는

map.resources :posts, :as => 'news'    #case 3 

map.resources :posts (사례 1)을 대체했다. 그것도 작동하지 않습니다.

어떻게하면됩니까?


EDIT :

rake routes의 출력 (에만 제 선)이다

: 케이스 2

   posts GET /posts       {:action=>"index", :controller=>"posts"} 
    formatted_posts GET /posts.:format     {:action=>"index", :controller=>"posts"} 
        POST /posts       {:action=>"create", :controller=>"posts"} 
        POST /posts.:format     {:action=>"create", :controller=>"posts"} 
      new_post GET /posts/new      {:action=>"new", :controller=>"posts"} 
    formatted_new_post GET /posts/new.:format    {:action=>"new", :controller=>"posts"} 
      edit_post GET /posts/:id/edit     {:action=>"edit", :controller=>"posts"} 
formatted_edit_post GET /posts/:id/edit.:format   {:action=>"edit", :controller=>"posts"} 
       post GET /posts/:id      {:action=>"show", :controller=>"posts"} 
     formatted_post GET /posts/:id.:format    {:action=>"show", :controller=>"posts"} 
        PUT /posts/:id      {:action=>"update", :controller=>"posts"} 
        PUT /posts/:id.:format    {:action=>"update", :controller=>"posts"} 
        DELETE /posts/:id      {:action=>"destroy", :controller=>"posts"} 
        DELETE /posts/:id.:format    {:action=>"destroy", :controller=>"posts"} 

출력 :

경우 1

3

  news_index GET /news       {:action=>"index", :controller=>"posts"} 
formatted_news_index GET /news.:format     {:action=>"index", :controller=>"posts"} 
         POST /news       {:action=>"create", :controller=>"posts"} 
         POST /news.:format     {:action=>"create", :controller=>"posts"} 
      new_news GET /news/new      {:action=>"new", :controller=>"posts"} 
    formatted_new_news GET /news/new.:format    {:action=>"new", :controller=>"posts"} 
      edit_news GET /news/:id/edit     {:action=>"edit", :controller=>"posts"} 
    formatted_edit_news GET /news/:id/edit.:format   {:action=>"edit", :controller=>"posts"} 
       news GET /news/:id      {:action=>"show", :controller=>"posts"} 
     formatted_news GET /news/:id.:format    {:action=>"show", :controller=>"posts"} 
         PUT /news/:id      {:action=>"update", :controller=>"posts"} 
         PUT /news/:id.:format    {:action=>"update", :controller=>"posts"} 
         DELETE /news/:id      {:action=>"destroy", :controller=>"posts"} 
         DELETE /news/:id.:format    {:action=>"destroy", :controller=>"posts"} 

내가 헥타르 예 2의 경우 소스 코드에 edit_news이 없기 때문에 오류가 발생합니다. <%= link_to 'Edit', edit_post_path(post) %>

+0

'레이크 루트'의 출력을 추가 할 수 있습니까? –

+0

'map.resources : posts, : as => 'news'' 그리고'/ news'에 접근하려고하면 어떻게 될까요? –

+0

@Marcel, 출력을 더했습니다. @Alex,이 경우'Routing Error - No route matches "/ news"를 {: method => : get}'으로 얻지 만'posts'와 함께 작동합니다. –

답변

0

첫 번째 단계는 좋았습니다. 모든 소식 (*) 클래스를 News * 클래스로 바꿉니다. 모델 호출하기 News and controller NewsController는 문제가되어서는 안됩니다. 당신은 또한 (또한 post_path 같은 것들이 news_path로 대체해야) 코드에서, 모든 항목을 찾을 수 있는지 확인합니다.

다음으로,가 PostsController 대신 NewsController가를 사용하기 위해

map.resources :news 

에 경로를 수정합니다. 그리고 그것은 효과가있다.

는 : 웹 서버를 다시 시작하는 것을 잊지 마십시오.

+0

I 당신이 말했듯이'routes.rb'에': singular => : news_instance'를 추가하고'new_news_path'를'new_news_instance_path'로 대체해야했습니다. 아직 해결해야 할 몇 가지 문제가 있습니다. 그러나이 페이지에서 'productos'를 사용한 예가 효과가없는 이유를 모르겠습니다. http://api.rubyonrails.org/classes/ActionController/Resources.html#M000522 –

6

단어 "뉴스"는 단수 (회원)과 복수 (컬렉션) 이름 사이의 모호함이있다. 이 문제를 해결하기 위해 노력할 수도 있지만 결국 혼란 스러울 수 있습니다. ("news_path"회원의 ID 매개 변수를 기대하거나 컬렉션 경로입니다해야 "뉴스"무엇입니까?)

그들에게 게시물을 전화로의 스틱 보자

map.resources :posts, :as => "news", :singular => "news" 
  • 귀하의 경로 "/ news"가됩니다
  • 컨트롤러가 PostsController가됩니다
  • 경로 도우미가 posts_path, post_path, edit_post_path 등이됩니다.

즉, 모든 경우에 자원을 "게시물"이라고 부르지 만 "/ 뉴스/*"아래의 경로에는 표시됩니다.

+0

당신 말이 맞습니다. 나는 이제 같은 단수형과 복수형으로 된 이름을 피한다. 그것들을 재정의 (또는 영어 이외의 이름을 설정)하고 싶을 때,'config/initializers/inflections.rb'에 새로운 굴절을 추가합니다. –