简体   繁体   中英

Rails routes to set of resources?

A typical Rails route like:

 resource :customer

Sets up routes for index, show, etc.

To access all the customers, I can use the route

/customers

To access a single customer, I use:

/customers/1

Whats the most concise, RESTful way to create a route and controller action that would allow a user to view a set of customers, ie something like

/customers/[1,2,3,4,5]

Updated with rationale

The use case:

  • This action will be called via Javascript to provide details on selected users. If a user selects 3 customers through the interface, I would like one request to pull info for all 3, instead of having to make 3 separate requests

You could just keep your routes simple: resources :customers .

The list of ids would be considered as the :id by the routes, and you would get [1, 2, 3, 4] in params[:id] .

Then in your controller, in a before_filter for example, you can check if the id matches a certain regexp (like... /\\[(\\d,)*\\d\\]/ ), and if it's the case you can extract the ids.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM