Statement
Imagine we have an inventory app, there it is a Movement model, it represent any movement like products purchase or products sale. So we have default REST routes.
movements GET /movements(.:format) {:action=>"index", :controller=>"movements"}
POST /movements(.:format) {:action=>"create", :controller=>"movements"}
new_movement GET /movements/new(.:format) {:action=>"new", :controller=>"movements"}
edit_movement GET /movements/:id/edit(.:format) {:action=>"edit", :controller=>"movements"}
movement GET /movements/:id(.:format) {:action=>"show", :controller=>"movements"}
PUT /movements/:id(.:format) {:action=>"update", :controller=>"movements"}
DELETE /movements/:id(.:format) {:action=>"destroy", :controller=>"movements"}
For mnemonic proposes, we want to have some descriptive routes, like:
new_purchase /purchase/new(.:format) {:controller=>"movements", :action=>"new_purchase"}
edit_purchase /purchase/:id/edit(.:format) {:controller=>"movements", :action=>"edit_purchase"}
If you can see purchase‘s are same model likemovement‘s, actually are processed by MovementsController, but there with have different flow and treatment, this specified by create_purchase instead of create.
Questions
-
How should I add restful routes for
purchase‘s? Taking care of specify HTTP methods likeGET,POST,PUT,DELETE, etc. -
How should I write
form_fortags? Usingmovementmodel we can write:<%= form_for(@movement) do |f| %>but how is to callpurchasepaths forcreateorupdatemethods? -
How should I specify validation rules for
purchase‘s? I have specified some rules onMovementmodel, but they are not applied forpurchase‘s when a form is submitted.
You could use some thing like this in your routes file
and you can mention others like delete, put in :via
for more info look at this link
If you want to change create to create_purchases go to the movements controller and change the definition names.
for the second question you could do something like this