2011-11-16 12 views
0

データベーステーブルのtypeカラムに従って、URLに接尾辞を追加したいと考えています。Railsルートのカスタム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 

代わりに、異なるタイプのために複数のページを作成する:RESTfulなないnearby_country.html.erbnearby_state.html.erbnearby_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を与える必要があります。