简体   繁体   中英

How to add parameters in URL of Ruby on Rails?

I wrote in routes.rb like this:

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:

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. Simply create the route to your controller#action, eg)

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. You are not required to mention them explicitly in the routes.

So just pass parameters into your url helper:

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"......)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM