url_for和remote:true

时间:2015-01-26 11:14:35

标签: ruby-on-rails ruby ajax forms

我遇到了Rails的问题。我想要一个选择的表单,在单击时呈现部分视图。我希望部分视图在不刷新整个文档(Ajax)的情况下有效,但似乎remote: true对我不起作用......

我目前拥有并刷新整个文档:

= form_tag url_for(action: :index), method: 'get' do
    //Storing some informations in html fields for further use
    - shops_array = @shops.collect.with_index { |shop, i| [shop.title, shop.id, {'address' => [shop.address, shop.zipcode, shop.city].join(" ")}, {'id' => 'shop'+String(i)}] }
    = select_tag :shop_id, options_for_select(shops_array, @shop_id), prompt: "Your shop :", data: {submit_on_change: true}

我尝试了什么:

= form_tag url_for(action: :index), remote: true, method: 'get' do
    //Storing some informations in html fields for further use
    - shops_array = @shops.collect.with_index { |shop, i| [shop.title, shop.id, {'address' => [shop.address, shop.zipcode, shop.city].join(" ")}, {'id' => 'shop'+String(i)}] }
    = select_tag :shop_id, options_for_select(shops_array, @shop_id), prompt: "Your shop :", data: {submit_on_change: true}

如何将remote: true选项应用于url_for?如果不可能,我可以使用一些帮助找到解决方法。

2 个答案:

答案 0 :(得分:0)

根据docs,您可以为remote方法指定form_tag选项,如下所示:

form_tag(url_for(controller: :charmander, action: :ember), remote: true, method: :get, class: 'pokemon-form') do
  # form logic
end

然而,这只会在提交整体表单时触发ajax:success事件。

在你的问题中,在我看来你想要在你的选择值改变时重新渲染一些部分。我不确定你是如何使用submit_on_change数据属性的,但是如果我认为你在选择vaule改变时让它提交整个表单是正确的那么你需要做的就是监听{{ 1}}整个表单的事件,如下:

ajax:success

答案 1 :(得分:0)

使用onclick和JavaScript函数结束。虽然它可能不是最好的解决方案,也不是更漂亮,但它对我来说就像是一种魅力。

= select_tag :shop_id, options_for_select(shops_array, @shop_id), prompt: "Your shop :", onclick: 'ajaxSelectedShop('+String(8)+')'