link_to params获取

时间:2014-03-29 20:09:40

标签: ruby-on-rails ruby ruby-on-rails-4

我必须创建像这样的链接

= link_to vacancy.new_candidates_count, hiring_candidates_path(???)

在哪里???一些参数。

如何使用这样的参数重定向到hiring_candidates_path

 Parameters: {"grid_filter"=>{"vacancies"=>["1"]}

在页面上hiring_candidates我可以使用过滤器,我可以在终端看到我发送的所有参数:

Started GET "/hiring/candidates.json?grid_filter%5Bgroup%5D=&grid__search=false&grid_nd=1396122997602&grid_rows=30&grid_page=1&grid_sidx=id&grid_sord=desc&grid_filter%5Bvacancies%5D%5B%5D=1" for 127.0.0.1 at 2014-03-29 21:56:37 +0200
Processing by Hiring::CandidatesController#index as JSON
  Parameters: {"grid_filter"=>{"group"=>"", "vacancies"=>["1"]}, "grid__search"=>"false", "grid_nd"=>"1396122997602", "grid_rows"=>"30", "grid_page"=>"1", "grid_sidx"=>"id", "grid_sord"=>"desc"}

等待你的帮助)

2 个答案:

答案 0 :(得分:2)

您可以将其作为query params传递:

= link_to vacancy.new_candidates_count, hiring_candidates_path("grid_filter"=>{"vacancies"=>["1"]})

点击后,此链接将创建一个包含查询参数的网址,如

/hiring/candidates.json?grid_filter%5Bvacancies%5D%5B%5D=1

答案 1 :(得分:0)

除了Kirti的帖子,你可能希望在你的路线中定义参数(称为bound parameters):

#config/routes.rb
resources :hiring do
   collection do
       get "candidates(/:vacancies)", action: :candidates
   end
end

这将允许您加载"裸体"数据(例如/candidates/1)。显然这不会直接适合你的问题,但它在未来肯定会有所帮助


您可能还想查看JSON query constructs以了解JSON路径的工作原理:

url/?param1=xxx&param2=xxx