簡體   English   中英

如何在Ruby on Rails的URL中添加參數?

[英]How to add parameters in URL of Ruby on Rails?

我在routes.rb中這樣寫:

get 'app/:name&:target' => 'apps#show', :as => :app_info

我想訪問“ app / myapp&mytarget”,然后轉到apps_controller#show,這是haml:

link_to app_item[:name], app_info_url(app_item[:name], app_item[:target])

但它返回:

No route matches [GET] "/app/myapp&mytarget"

我在這里錯過了什么?

您無需指定要在路由中接受的URL參數。 只需創建到您的controller#action的路由,例如)

get 'app/:name' => 'apps#show', :as => :app_info

並且在您的apps#show動作中,可以從params [:target]中訪問URL參數,這樣的URL參數可能很多,並且它們都將位於rails為您生成的params哈希中。 您無需在路由中明確提及它們。

因此,只需將參數傳遞到您的url幫助器中:

link_to app_item[:name], app_info_url(app_item[:name], target: app_item[:target])

在路徑或網址中這樣傳遞

app_info_url(app_item[:name], app_item[:target], :params1 => "value1", params2 => "value2"......)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM