簡體   English   中英

rails,如何從link_to助手中傳遞變量

[英]rails, how to pass a variable from a link_to helper

在我的routes.rb中,我有

match "monsters/:category" => "monsters#index"

我有一堆鏈接,例如

  • 地球
  • 飛行

但是我無法通過“ Water”或“ Fire”作為我的路線的:category。 我該怎么做?

我嘗試過類似...

<%= link_to "Water", "monsters/water"(:category => "water") %>

但從語法上講,我不能做到這一點。 如何將“水”作為:category符號傳遞?

謝謝!

您無需在link_to中指定: category 在執行此操作時:

<%= link_to "Water", "monsters/water" %>

您的路線會將水與:category參數匹配,因此,在您的控制器中,您可以訪問params[:category]將成為值。 請注意,在路由中您已經說過,“ monsters /”之后的任何內容都將被識別為category參數。

注意,如果您具有Named Routes ,則可以傳遞如下參數:

<%= link_to "Up", monster_path(:category => "water") %>

暫無
暫無

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

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