简体   繁体   中英

Ruby on Rails: routes and resources

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?

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM