2011-11-16 2 views
0

내 데이터베이스 테이블의 type 열에 따라 URL에 접미어를 추가하고 싶습니다.레일 경로의 사용자 정의 URL

# shop_controller.rb 
... 
def near 
    @shop = Shop.find(params[:id]) 
    @type = @shop.type 
end 

# routes.rb 
resources :spots do 
    member do 
    get :near 
    end 
end 

페이지 내가 현재 app/views/shops/nearby.html.erb에 있습니다. 생산되는 URL이 현재 :

http://localhost/shops/1/near 

대신 서로 다른 유형의 여러 페이지 생성 : nearby_country.html.erb, nearby_state.html.erb을 편안하고없는 nearby_city.html.erb, 내가 유형별로 상점을 필터링하기 위해 URL에 유형을 추가하는 것을 선호 미래에 내가 더 많은 종류가있을 때 있도록 자동으로 표시 할 수 있습니다 :

nearby_country_shop_path 
nearby_state_shop_path 
nearby_city_shop_path 
:

http://localhost/shops/1/near_country 
http://localhost/shops/1/near_state 
http://localhost/shops/1/near_city 

는 또한 싶어처럼 내 메뉴의 사용자 지정 경로를 할 수있을3210

누구든지 몇 가지 힌트를 주실 수 있습니까? 감사!

답변

1

페이지의 섹션 3.1 : 또한

http://localhost/shops/1/near?country 
http://localhost/shops/1/near?state 
http://localhost/shops/1/near?city 

참조 :

http://guides.rubyonrails.org/routing.html

는하지만, 당신은 아마 같은 경로를 사용해야합니다

http://railscasts.com/episodes/203-routing-in-rails-3

http://railscasts.com/episodes/231-routing-walkthrough

http://railscasts.com/episodes/232-routing-walkthrough-part-2

+0

어쨌든 밑줄을 붙이시겠습니까?'near_country'? – Victor

+0

사실, 이미'show.html.erb'에 대한 국가가 이미 있기 때문에 – Victor

1

이것은 당신이 요청하지만 예를 들어 경로로 매개 변수 x을 추가 할 수 있습니다 정확히하지 않습니다. 같은

near_spot GET /spots/:id/near/:x(.:format) {:action=>"near", :controller=>"spots"} 

.. 및 URL 헬퍼 :

near_spot_path(spot_id, :country) 
near_spot_path(spot_id, :state) 

/spots/1/near/country 
/spots/1/near/state 
/spots/1/near/city 

.. 및 경로와 같은 :

resources :spots do 
    member do 
    get 'near/:x', :action => :near, :as => :near 
    end 
end 

이렇게하면 같은 URL을 제공해야 .