Rails 4 nested routes - ruby-on-rails

routes.rb
Rails.application.routes.draw do
root to: 'visitors#index'
resources :states do
resources :cities do
get 'listings'
end
end
end
I am looking to have my GET URL set up like:
../state.id/city.id/listings.id
I am using friendly_id so the urls will read like:
../OR/Portland/2011-ford-truck

Listing is it's own model (resource) too in this case. You will also need a resources for listing. If it only has a show action, you can limit it like this:
resources :states do
resources :cities do
resources :listings, only: [:show]
end
end

Related

Rails nested resources ignore single route

I have a rails nested resource in my routing.
i.e
resources :users do
resources :accounts
end
resources :accounts
The listing operations of course will be:
GET /users
GET /users/:user_id/accounts
I want to get rid of the /users route but retain the /users/:id/accounts route.
Any idea how I can go about this? Thanks
Using except: [:index] not will restrict both routes. Thats a nonsenical claim that can easily be refuted by just running rails routes. None of the options for resources "trickle down" to nested calls.
resources :users, only: [] do
resources :accounts, only: :index
end
only: [] skips generation of all the "user" routes.
This will generate the routes:
Prefix Verb URI Pattern Controller#Action
user_accounts GET /users/:user_id/accounts(.:format) accounts#index
# ...
Note that the param key is :user_id and not :id. If you REALLY want to break the conventions you would need to do:
# don't do this - its stupid
scope '/users/:id', as: :user do
resources :accounts, only: :index
end
let set only: [] then rails routes will generate /users/:id/accounts as you want
resources :users, only: [] do
resources :accounts # , only: [:index] if you just only keep users/:id/accounts
end
# if you only want to get rid of GET /users
resources :users, except: [:index]
# if you mean you want to get rid all of /users routes (not just only GET /users) then comment above line
This is what scope is for
scope :users do
resources :accounts
end
Rails guides on routing
Or you can use these same two ways, use namespace with scope:
namespace :users do
scope ':user_id' do
resources :accounts
end
end
Use just only scope:
scope ':users/:user_id' do
resources :accounts
end

Nested routes and url helper in Rails 4

I have nested routes which goes something like this:
resources :venues do
#Halls
get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition
get "hall/:id/visit" => "halls#visit", as: :hall_visit
get "venue_structure", :to => "venues#venue_structure"
resources :asset_types, :booths_tags, :tags, :uploaded_files, :events, :chats
resources :halls do
resources :webcasts
resources :booths do
resources :chats
end
end
end
Problem with this approach is that I have to put in three paramters in url helpers for nested ones like below:
venue_hall_booth_path(#booth.hall.venue, #booth.hall, #booth)
Is there a better approach to doing this other than me having to put in three different resources as parameters each time when I use this helper?
You can use shallow routes:
resources :halls, shallow: true do
resources :webcasts
resources :booths do
resources :chats
end
end
This allows you access member urls without having to use the parent. Unless it's a new or create actions.
Or you can define them separately.
resources :booths do
resources :chats
end
resources :halls do
resources :webcasts
resources :booths
end

Rails 3 create a route to nested independent page

I have 3 levels of nesting.
routes.rb looks like this
resources :clients do
resources :departments do
resources :tasks
end
end
I would like to create a custom path that looks like this
/clients/:client_id/departments/:department_id/tasks/data
I have tried adding the following
resources :clients do
resources :departments do
resources :tasks
member do
get "data"
end
end
end
This creates the route
/clients/:client_id/departments/:department_id/tasks/:task_id/data
How would I remove the :task_id part the path?
A member route acts on a member, that's why it requires an id. A collection acts on a collection and so doesn't require an id.
resources :clients do
resources :departments do
resources :tasks do
collection do
get "data"
end
end
end
end
You should use
resources :clients do
resources :departments do
resources :tasks
get "data", :on => :collection
end
end

Adding a Route to the base URL of plural Resources

In Ruby on Rails, is there a way to add another RESTful action to the base URL of a plural resource? I'm looking for something like this:
resources :groups do
resources :users do
put on: :base, to: 'users#update_all'
end
end
Which would generate the route: [PUT] groups/:group_id/users => users#update_all
I've already tried doing this:
resources :groups do
resources :users
put 'users', on: :member, to: 'users#update_all'
end
But that doesn't preserve the value of params[:group_id] in the controller.
resources :users do
collection do
put '' => 'users#update_all' ## PUT /users
end
end
UPDATE
It would be recommended to do this though:
resources :users do
collection do
put 'update_all' ## PUT /users/update_all
end
end
Both route to the update_all action of the users controller.
RESOURCES
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Route conversion for map.resources in rails 3

Currently, I have:
map.resources :users do |user|
user.resources :blogs
end
How do I turn this into a match or resources in rails 3+?
My attempt would be:
resources :users do
user.resources :blogs
end
I got most of my information on route changes from here.
resources :users do
resources :blogs
end
Since you do not have |user| in the Rails 3 block, you cannot refer to it. In addition, you shouldn't need to:
resources :users do
resources :blogs
end
Rails routing guide explains this. You should be able to use
resources :users do
resources :blogs
end

Resources