EastsideDev
EastsideDev

Reputation: 6639

Getting partial routes

In my config/routes.rb, I have:

resources :landings do
  collection do
    get 'about'
  end
end

Which give me the following routes:

about_landings  GET     /landings/about(.:format)       landings#about
landings        GET     /landings(.:format)             landings#index
                POST    /landings(.:format)             landings#create
new_landing     GET     /landings/new(.:format)         landings#new
edit_landing    GET     /landings/:id/edit(.:format)    landings#edit
landing         GET     /landings/:id(.:format)         landings#show
                PATCH   /landings/:id(.:format)         landings#update
                PUT     /landings/:id(.:format)         landings#update
                DELETE  /landings/:id(.:format)         landings#destroy

I only need the about route, and possibly a couple of other static pages routes. What is the routes.rb syntax for that?

Upvotes: 0

Views: 118

Answers (2)

jvillian
jvillian

Reputation: 20253

You could do something like:

resources :landings, only: [] do
  collection do
    get 'about'
  end
end

Which will give you:

about_landings GET    /landings/about(.:format)  landings#about

Personally, I don't like about_landings as a path name (aesthetically), so I think I would do:

scope module: :landings do
  get 'about'
end

Which will give you:

about GET    /about(.:format)  landings#about

And then you can just use about_path which IMO is nicer (and, you type fewer characters while building routes.rb thereby adding fractional seconds to your overall lifespan). Also, you get a cleaner (again, IMO) url in the browser address bar.

Upvotes: 1

krishna raikar
krishna raikar

Reputation: 2675

You can use except / only

resources :landings, except: [:show, :new, :edit] do
  collection do
    get 'about'
  end
end

OR

resources :landings, only: [:index] do
  collection do
    get 'about'
  end
end

NOTE: you can skip or allow only specific action.I just given example

Upvotes: 3

Related Questions