简体   繁体   English

如何在Ruby on Rails的URL中添加参数?

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

I wrote in routes.rb like this: 我在routes.rb中这样写:

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

I want to visit "app/myapp&mytarget" and then go to apps_controller#show, this is the haml: 我想访问“ app / myapp&mytarget”,然后转到apps_controller#show,这是haml:

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

but it returns: 但它返回:

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

What do I missed here? 我在这里错过了什么?

You don't need to specify the URL parameters you wish to accept in the routes. 您无需指定要在路由中接受的URL参数。 Simply create the route to your controller#action, eg) 只需创建到您的controller#action的路由,例如)

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

and inside your apps#show action access the URL parameters from within params[:target], there can be numerous such URL parameters and all of them will be within the params hash that rails generates for you. 并且在您的apps#show动作中,可以从params [:target]中访问URL参数,这样的URL参数可能很多,并且它们都将位于rails为您生成的params哈希中。 You are not required to mention them explicitly in the routes. 您无需在路由中明确提及它们。

So just pass parameters into your url helper: 因此,只需将参数传递到您的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