0

Rails 3.2. Here, I wanna direct all http://domain.dev/toys to only show all shops which shop_type (in my table column) is toys.

# routes.rb resources :shops match 'toys' => 'shops#index', :as => :toys, :via => :get, :constraints => {:shop_type => 'toys'} # shops_controller.rb def index @shops = Shop.find(:all) end 

What have I done wrong? Thanks.

0

2 Answers 2

2

The wrong part: Shop.find(:all).

Constraints are for route segments.

(Well, and verbs, but then they're specified with :via; or methods on Request.)

Sign up to request clarification or add additional context in comments.

2 Comments

Mind to share some ideas how to improve it?
@Victor You need to do a find with the appropriate shop type--not sure what the cleanest way to do it is, but the easiest is to just create a method that does so, call it, and render the appropriate template.
0

In routes.rb:

match 'shops(/:shop_type)' => 'shops#index', :via => :get, :as => :shops_path 

In shops_controller.rb:

SHOP_TYPES = [:toys, :clothing, :accessories] def index @shops = [] if SHOP_TYPES.include? params[:shop_type].to_sym @shops = Shop.find_all_by_shop_type(params[:shop_type]) else @shops = Shop.find(:all) end end 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.