【问题标题】:Rails form_for collection_select ignoring remote ajax call that select_tag acceptsRails form_for collection_select 忽略 select_tag 接受的远程 ajax 调用
【发布时间】:2012-03-13 08:57:15
【问题描述】:

在让我的表单助手工作之前,我使用以下内容作为我的选择下拉菜单:

<%= 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")
                }
%>

这完美地调用了我的 filter_by_city.js.erb 并更新了一些其他值。用萤火虫检查表明它具有数据远程等属性。

但是,更改为下面的 form_for 助手,我没有得到数据远程,因此没有 AJAX 调用。

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

下拉列表的显示与以前完全相同(并且需要对参数进行一些混淆才能获得它),但它除了设置参数值之外没有其他功能。

我尝试将 :data 包装在 {} 中(来自法国论坛 here,但这不是解决办法。

我认为这是一个新手错误,但如果找到它的任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: ruby-on-rails ajax forms


    【解决方案1】:

    ::叹息::

    使用 collection_select 时,与 RoR 的某些部分不同,按顺序包含所有参数似乎有点挑剔。尽管我可以(并继续)省略 :post,但解决方案结果是包含一个空的选项集,并在 {} 中为 html_options 包装 :data。

    所以,这就是有效的:

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

    注意 :name 后面的 {} 和开始下一行的 {。

    外卖消息 - 如果包含 html_options,则 OPTIONS 不是可选的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      相关资源
      最近更新 更多