Ruby on Rails: routes and resources

Go To StackoverFlow.com

1

I have a Jobs resource and every job has a location. How do I create a route, which would show jobs by location? In other words, how to get this: /jobs/locations/london

The Location is a separate model itself.

I tried:

  resources :jobs do
    collection do
      match 'locations/:id' => 'jobs#find_by_location', as: :jobs_find_by_location, via: :get
    end
  end

But this doesn't work:

link_to @location.name, jobs_find_by_location(@location)

What would be be cleanest Rails way of doing this?

2012-04-04 08:16
by krn


0

Try

match '/jobs/locations/:location_name', :controller => 'locations', :action => 'action_name', :as => jobs_find_by_location, :via => :get

OR

match '/jobs/locations/:location_name' => 'locations#action_name', :as => jobs_find_by_location, :via => :get

Restart the server and do the request /jobs/locations/london

In your action use params[:location_name] to get the 'london'

2012-04-04 09:03
by Vik
Ads