简体   繁体   English

Rails form_for collection_select忽略select_tag接受的远程ajax调用

[英]Rails form_for collection_select ignoring remote ajax call that select_tag accepts

Before getting my form helper working, I was using the below for my select dropdown: 在让表单助手工作之前,我使用下面的选择下拉列表:

<%= select_tag :city_id, 
        options_for_select( City.all.collect{|c| [c.name,c.id]} ),
        :data => {  :remote => true,
                    :url => url_for(:controller => "locations", 
                                    :action => "filter_by_city")
                }
%>

and this worked perfectly to call my filter_by_city.js.erb and update some other values. 这完全可以调用我的filter_by_city.js.erb并更新其他一些值。 Inspecting with firebug shows that it has the data-remote, etc, properties. 使用firebug进行检查表明它具有数据远程等属性。

Changing to the form_for helper below, however, I get no data-remote, and thus no AJAX call. 但是,更改为下面的form_for帮助程序,我没有数据远程,因此没有AJAX调用。

<%= f.collection_select(:city_id, 
        City.all, :id, :name,
        :data => {  :remote => true,
                    :url => url_for(:controller => "locations", 
                                    :action => "filter_by_city")
                }
    )
%>

The dropdown appears exactly as before (and it took some muddling with the parameters to get that), but it has no functionality other than setting the params value. 下拉列表与之前完全一致(并且参数得到了一些混乱),但它除了设置参数值之外没有任何功能。

I tried wrapping :data in {} (as from a french forum here but that wasn't the cure. 我尝试包装:{}中的数据(来自法国论坛但这不是治愈方法。

I assume it's a newbie mistake, but any help finding it will be most appreciated. 我认为这是一个新手的错误,但任何帮助找到它将是最受欢迎的。

Thanks 谢谢

::le sigh:: :: le叹::

When using collection_select, unlike some parts of RoR, it seems it's a bit picky to include all arguments in order. 当使用collection_select时,与RoR的某些部分不同,看起来按顺序包含所有参数似乎有点挑剔。 Even though I can (and continue) to leave out :post, the solution turned out to be including an empty set for options, and wrapping :data in {} for html_options. 即使我可以(并继续)遗漏:post,解决方案结果是包含一个空集的选项,并包装:{} in {} for html_options。

And so, this is what works: 所以,这是有效的:

<%= f.collection_select(:city_id, 
        City.all, :id, :name, {},
        {:data => {  :remote => true,
                    :url => url_for(:controller => "locations", 
                                    :action => "filter_by_city")
                }
        }
    )
%>

Note the {} after :name and the { starting the next line. 注意{}之后:name和{开始下一行。

Takeaway message - OPTIONS are NOT OPTIONAL if including html_options. 外卖消息 - 如果包含html_options,则选项不可选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM