开发者

Create a routes to a controller without model with a /id

I have product with a foreign collection_id key

I want to pass an id to a controller. To do so i have the following routes for my controller :

controller :magasin do

  get "magasin" => "magasin#index"
end

The only view in my controller is magasin/index.html.erb

The link to magasin is link_to c开发者_如何学Goollection.nom, magasin_path(collection)

This kind of syntax usually works in controllers with models. Here my link is : http://localhost:3000/magasin.2 instead of http://localhost:3000/magasin/2

Later on i will need to call the same view with product_kind_id instead of collection_id and i will add sort by name/price ....

How can i have the ID as a normal argument (/:id)instead of a type(.id)?


A popular URL schema to follow is RESTful routing. Rails has this built-in and will set it up for you if you initialize your resource via rails generate scaffold Magasin nom:string.

If you put resources :magasins in your routing file, it will route /magasins to MagasinsController#index and /magasins/1 to MagasinsController#show with params[:id] set to "1". It will also set up a few other routes that will be useful in the future but for now will just raise an action not found exception.

You don't want to use the dot as an argument delimiter, since Rails places what comes after the dot in the request.format method or just in params[:format] (ordinarily accessed through the respond_to method that comes with the generated scaffolds). Save that dot for later when you are working on delivering alternative display formats like XML and JSON.

I realize I've said a lot in a small space, so feel free to ask any follow up questions once you've consulted the Rails Guide on the issue, and I'll be very glad to help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜