When I’m generating controller with methods in routes I have something like this:
get "vehicle_manufacturers/show"
Is it a good practice that after that I will write such code:
match 'vehicle_manufacturers/:id/' => 'vehicle_manufacturers#show', :as => :vehiclemanufacturers
Or, is there another way of writing this code so that will work properly?
It’s best practice to follow Rails conventions for naming and routing to your actions. This lets you say:
This will automatically create routes for
index,new,create,show,edit,update, anddeletewith the appropriate HTTP methods and helper names. Assuming it corresponds to aVehicleManufacturerobject that conforms to ActiveModel conventions (like ActiveRecord, Mongoid, etc.),url_forwill automatically Do The Right Thing™, letting you use forms and redirects and such with no extra routing-related work.It’s pretty common that you might only want a subset of these methods. That’s fine too:
You want to support additional methods outside the normal CRUD methods. Go for it:
This would add a route named
stock_price_vehicle_manufacturermapping to/vehicle_manufacturers/:id/stock_price. Although, strictly speaking, I might consider a stock price to be a sub-resource…The point is, try to use resources as the basis of your routing. It makes everything easier.