Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single custom param name in routes for nested resources Rails 4.1

I have question about param name for nested resources in rails routes For example i have:

resources :controller1, param: :controller_id do
  resources :controller2
end

and i have routes:

controller1/:controller_id/
...
controller1/:controller_controller_id/controller2/...
...

I want single :controller_id for controller1 I know it's looks bad, but How do this? Thanks!

like image 540
Mikhail Avatar asked Jun 11 '14 10:06

Mikhail


People also ask

What is resource routing in rails 2?

2 Resource Routing: the Rails Default. Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

Can rails create paths and URLs from an array of parameters?

In addition to using the routing helpers, Rails can also create paths and URLs from an array of parameters. For example, suppose you have this set of routes:

How do I customize the default routes and helpers in rails?

While the default routes and helpers generated by resources will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers. The :controller option lets you explicitly specify a controller to use for the resource.

What is each method in resource route in rails?

Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller. When your Rails application receives an incoming request for: it asks the router to map it to a controller action. If the first matching route is:


1 Answers

how about this:

resources :controller1, param: :controller_id do
  member do
    resources :controller2
  end
end

will generate

GET    /controller1/:controller_id
GET    /controller1/:controller_id/controller2
GET    /controller1/:controller_id/controller2/:id
...
like image 130
calfzhou Avatar answered Oct 16 '22 22:10

calfzhou