URL本身的URL参数而不是之后?在Rails中

时间:2012-05-21 23:33:34

标签: ruby-on-rails-3 forms routes url-parameters

我在路线中有以下一行:

match 'area/:area_id/stores', :to => "stores_directory#index", :as => "stores_directory"

我有一个表单,我可以选择area_id作为:

<%= form_tag stores_directory_path, :method=>:get do %>

<select id="area_id" name="area_id">
....
</select>

这会将area_id作为参数广告?所以,我的问题是:如何在区域商店之间添加它?

1 个答案:

答案 0 :(得分:0)

我看到一个解决方案(不是最好的解决方案,但也非常可接受)。可以编写自己的中间件来处理这个请求,但这是我认为的错误方法(在这种情况下)。

相反,使用JS重定向

这可以写成一个方法(使用JQuery的例子):

$('#area_id option').click(function() {
  if (this.value) { // check value is not empty
    document.location.href = Routes.stores_directory_path({area_id: this.value})
  }
});

或使用options_for_select:

options = []
areas.each do |a|
  options << [a.to_s, a.id, {:onclick => "document.location.href = Routes.stores_directory_path({area_id: #{a.id}})"}]
end
options_for_select(options)

在这里,我使用了js-routes gem作为不错的网址。

希望这会有所帮助。