0

Some routes are for CRUD operations, such as client, phone_numbers and products. I have 'unique-index' attributes for many of the models. For example, in Client, I could have "email" or "username", whereas in Products I could have "serialnumber", and in PhoneNumber the actual "number" could be unique-index.

So, I have methods in my ApplicationController that receives a JSON in a POST request, with the attribute name and attribute value. The server checks if the value exists and informs the user if the input is valid or if value exists.

So, for these models, I have to declare a route that points to the "unique" method, such as below

routes.rb

resources :clients
resources :phone_numbers
resources :products
post 'clients/unique' => 'clients#unique'
post 'phone_numbers/unique' => 'phone_numbers#unique'
post 'products/unique' => 'products#unique'

My question is: Can I "group" these routes, without prefix (unlike namespace and scope) to just add post 'unique' to them? a pseudo-code would be like this

pseudo-code

group alias: 'modelsWithUniqueAttrs'
    resources :clients
    resources :phone_numbers
    resources :products

    add_route 'unique'
end
Aleksandrus
  • 1,589
  • 2
  • 19
  • 31

2 Answers2

1

Try this

resources :clients, :phone_numbers, :products do
  collection do
    post :unique
  end
end

Or

unique_routing = Proc.new do
  collection do
    post :unique
  end
end
resources :clients, &unique_routing
resources :phone_numbers, &unique_routing
resources :products, &unique_routing

Or

unique_routing = Proc.new do
  collection do
    post :unique
  end
end
resources :clients, :phone_numbers, :products, &unique_routing
Johnny Dương
  • 753
  • 3
  • 8
0

You can define your routes like this:

resources :clients do
  member do
    post :unique => 'clients#unique'
  end
end

resources :phone_numbers do
  member do
    post :unique => 'phone_numbers#unique'
  end
end

resources :products do
  member do
    post :unique => 'products#unique'
  end
end

These will give you routes like:

unique_client           POST     /clients/:id/unique(.:format)                clients#unique
unique_phone_numbers    POST     /phone_numbers/:id/unique(.:format)          phone_numbers#unique
unique_products         POST     /products/:id/unique(.:format)               products#unique

Update

You can define routes using collection block like this:

  resources :clients, only: [:uniq] do
    collection do
      post :uniq
    end
  end

which you give you routes like:

uniq_clients POST /clients/uniq(.:format) clients#uniq

But, this is still not what you want exactly. Because, you have to have the similar collection blocks for products and phone_numbers as well.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • Unfortunately, I don't use /clients/:id/unique. I use /clients/unique, and pass a JSON, like {attrName: 'email', attrValue: 'test@test.com'}. I'd like a solution that doesn't require me to write 'controller_name#unique' for every controller... Instead, I'd like to group them to add 'unique' to all of them at once – Aleksandrus Aug 27 '15 at 01:20
  • Add a `collection` block, see [here](http://stackoverflow.com/a/3028670/3446742) and [here](http://guides.rubyonrails.org/routing.html#adding-collection-routes) – tangrufus Aug 27 '15 at 02:12