Incerteza
Incerteza

Reputation: 34884

Trying to simplify some parts of routes

I want to simplify this somehow:

  namespace :api do
    namespace :v1 do
      get ":domain/test1" => "home#test1", constraints: { domain: /[0-z\.]+/ }, defaults: { format: :json }
      get ":domain/test2" => "home#test2", constraints: { domain: /[0-z\.]+/ }, defaults: { format: :json }
      get ":domain/test3" => "home#test3", constraints: { domain: /[0-z\.]+/ }, defaults: { format: :json }
      # ........... and so on
    end
  end

Is there any way?

Upvotes: 0

Views: 43

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Perhaps you'd like to use some sort of method in your routes:

#config/routes.rb
def record id
   get ":domain/#{id}" => "home#test#{id}", constraints: { domain: /[0-z\.]+/ }, defaults: { format: :json }
end

namespace :api do
    namespace :v1 do
       x = 10
       x.times do |i|
           record i
       end
    end
end

Very bloated, memory wise, of course.

The killer is the :domain constraint. I was ready to just create a resources :home call, with some custom methodology inside - but you'll have to define the constraint manually each time you reference the route you require

Upvotes: 0

Pablo D
Pablo D

Reputation: 393

Maybe this can help you?

http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments

For your problem, you have to use:

get '/stories/:name', to: redirect('/posts/%{name}')

Upvotes: 1

Related Questions