Rails: Preserving GET query string parameters in link_to

前端 未结 8 2113
忘掉有多难
忘掉有多难 2020-12-02 15:22

I have a typical search facility in my app which returns a list of results that can be paginated, sorted, viewed with a different records_per_page value, etc. Each of these

相关标签:
8条回答
  • 2020-12-02 16:11
    link_to 'Link', params.merge({:per_page => 20})
    
    0 讨论(0)
  • 2020-12-02 16:13

    The simplest way to merge the new params with the query parameters and NOT with all parameters (including those obtained through the path) is to merge with request.query_parameters

    link_to 'Search', search_path(request.query_parameters.merge({ per_page: 20 }))
    

    Otherwise you end up with query strings duplicating the path parameters, for example ?action=index&controller=products&foo=bar instead of ?foo=bar.

    0 讨论(0)
提交回复
热议问题