0

我有一个页面,我需要在其中发送参数,以便 url 看起来像这样:

/my_controller?stores%5BStoreName%5D=1

但是,当我尝试形成我的时,link_to我得到了这个网址:

/my_controller?stores%5B%5D=5BStoreName&stores%5B%5D=1

这是我认为的 link_to 代码:

<%= link_to store, my_controller_path(:stores => [store, 1]) %>

如何更改我的代码,以便我使用与我需要的链接匹配的参数构建 url?

我的参数应该看起来像{"stores"=>{"StoreName"=>"1"},但现在它们看起来像{"stores"=>["StoreName", "1"]

控制器:

这是我的控制器中读取此哈希的索引方法 - 为清楚起见。

def index    
  @all_stores = Product.all_stores
  @selected_stores = params[:stores] || session[:stores] || {}

  if @selected_stores == {}
    @selected_stores = Hash[@all_stores.map {|store| [store, store]}]
  end
  if params[:stores] != session[:stores]
    session[:stores] = params[:stores]
    redirect_to :stores => @selected_stores and return
  end
  @products = Product.order("created_at desc").limit(150).find_all_by_store(@selected_stores.keys).group_by { |product| product.created_at.to_date}
  . . . etc

背景:

The bigger picture here is that the destination page (that the above link will link to) is a page that lists all products filtered by store. The way the filter normally works is with a set of checkboxes (the user can mark checkboxes to show products from specific stores). The desired link will take users directly to this page with the desired filter already applied - without having to mark checkboxes.

For additional reference, here is that checkbox helper that filters that destination page's products:

<%= check_box_tag "stores[#{store}]", 1, @selected_stores.include?(store), :id => "stores_#{store}" %>
4

1 回答 1

1

Use <%= link_to store, my_controller_path(:stores => { store => 1}) %>

于 2013-01-31T12:49:02.587 回答