从网址中提取多个参数

时间:2016-04-22 08:27:50

标签: ruby-on-rails model-view-controller parameter-passing

我有一个rails应用程序,我想从这个URL中提取2个不同的ID:

http://localhost:3000/shopping/suppliers/2/products/1

查看development.log,似乎两个值都存在。

我想知道如何将这些参数暴露给控制器,它只是params[:supplier_id]params[:id]吗?

Started GET "/shopping/suppliers/2/products/1" for 127.0.0.1 at 2016-04-22 18:28:19 +1000
Processing by Shopping::ProductsController#show as HTML
  Parameters: {"supplier_id"=>"2", "id"=>"1"}
  Product Load (30.1ms)  SELECT  "products".* FROM "products" WHERE "products"."permalink" = $1  ORDER BY "products"."id" ASC LIMIT 1  [["permalink", "1"]]
  Product Load (0.8ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1  [["id", 1]]
  User Load (19.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "products".* FROM "products" WHERE "products"."permalink" = $1  ORDER BY "products"."id" ASC LIMIT 1  [["permalink", "1"]]
  CACHE (0.0ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1  [["id", "1"]]
  Rendered shopping/products/show.html.haml within layouts/mdl (0.3ms)
  Rendered shared/_meta_data.html.haml (0.8ms)
  Rendered shared/_top_cart.html.haml (0.1ms)
  Rendered shared/_compact_menu.html.haml (4.5ms)
Completed 200 OK in 109ms (Views: 46.2ms | ActiveRecord: 50.8ms)

1 个答案:

答案 0 :(得分:2)

是的,您应该可以使用这些键访问您的值:

params[:supplier_id]
params[:id]

为什么在发布问题之前没有尝试过自己的建议,这很奇怪。 :)