Rails: Preserving GET query string parameters in link_to

前端 未结 8 2111
忘掉有多难
忘掉有多难 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 15:50

    If you want to keep existing params and not expose yourself to XSS attacks, be sure to clean the params hash, leaving only the params that your app can be sending:

    # inline
    <%= link_to 'Link', params.slice(:sort).merge(per_page: 20) %>
    

     

    If you use it in multiple places, clean the params in the controller:

    # your_controller.rb
    @params = params.slice(:sort, :per_page)
    
    # view
    <%= link_to 'Link', @params.merge(per_page: 20) %>
    

提交回复
热议问题