【问题标题】:respond to do format Issue回应做格式问题
【发布时间】:2014-03-28 08:30:01
【问题描述】:

我有 2 个字段,例如州和城市。城市将加载相对于相应的状态。而且我的字段不是纯粹的 DropDown。它们就像一个带有自动完成功能的输入字段,允许我们选择多个选项(参考图片..)

这就是我加载状态的方式。

<%= jl.select :state_id,  State.all.map{|j| [j[:name], j[:id]]}, {}, {class: 'select2', multiple: true, 'data-placeholder' => 'Type State to Search'} %>

为了加载城市,我编写了 jquery,它发送了一个 ajax 请求,然后将响应附加到它的成功函数中。

而我的控制器就像

def update_city_select
    @states = State.where(name: params[:name]).order(:id) unless params[:name].blank?    
    @cities = City.where(state_id: @states.first.id).order(:name) unless @states.first.id.blank?
    respond_to do |format|
      format.html { render layout: false }
      format.js
      format.xml
    end
  end

而我的 update_city_select.html.erb 就像

<%= select_tag 'city_id', options_from_collection_for_select(@cities, "id", "name", @states), {}, {class: 'select2', multiple: true, 'data-placeholder' => 'Type City to Search'} %>

问题是我的终端总是出错..

(wrong number of arguments (4 for 1..3)):
    1: <%= select_tag 'city_id', options_from_collection_for_select(@cities, "id", "name", @states), {}, {class: 'select2', multiple: true, 'data-placeholder' => 'Type State to Search'} %>

如果我删除 {} 表单 update_city_html 页面,它会正常工作。但我得到的只是一个简单的下拉列表,在我的第一张图片中没有像阿拉巴马加利福尼亚这样的输入字段。 有什么办法可以解决这个问题。

【问题讨论】:

    标签: ruby-on-rails ruby ajax ruby-on-rails-4


    【解决方案1】:

    我认为您的问题与您如何呈现表单有关,而不是 Rails 中的 respond_to 块。我看到你在使用select2


    select_tag

    我认为您的select_tag 没有被正确调用。这与您是否从respond_to 调用正确的content-type 无关

    这似乎很明显,但事实是您在调用 select_tag 时与您的表单无关,因此这将是问题的原因


    options_from_collection_for_select

    诚然,我对 select2 没有任何经验,但我一直在寻找,我认为您的问题可能与您的 options_from_collection_for_select 以及您的{}:

    <%= select_tag 'city_id', options_from_collection_for_select(@cities, "id", "name", @states), {}, {class: 'select2', multiple: true, 'data-placeholder' => 'Type State to Search'} %>
    

    我会测试仅将 id 传递给 options_from_collection_for_select 的最后一个参数(看看您发送整个对象是否会成为 Rails 的问题)


    选择

    正如Paul the octopus 所述,您调用select_tag 基本上意味着您只能使用have 3 arguments,而f.select 有4 个:

    select_tag(name, option_tags = nil, options = {})
    
    select(object, method, choices, options = {}, html_options = {})
    

    表面级别的修复将是使用带有新 select 标记的 form_builder 对象,而不是裸露的 select_tag

    #update_city_select.html.erb 
    <%= form_for @instace_var_for_form do |jl| %>
        <%= jl.select :city,  options_from_collection_for_select(@cities, "id", "name", @states), {}, {class: 'select2', multiple: true, 'data-placeholder' => 'Type State to Search'} %>
    <% end %>
    
    #ajax
    $(document).on("ajax:success", "select", function(data) {
        //pull select form form
        $("element").append(select);
    });
    

    【讨论】:

    • 其实我不明白。你能说得更具体一点吗?
    • 抱歉 - 我不小心点击了提交 - 我还在努力寻找答案!
    • ActionView::Template::Error (form 中的第一个参数不能包含 nil 或为空): 1: 2: 'Type State to Search'} %> 3: 在控制台中收到此错误:(
    • 对不起 - 我的意思是说我用它作为你在表单中使用的实例变量的占位符。您需要将@instance_var_for_form 替换为您在@form_for 中使用的实例变量,并可能在您的ajax 操作中再次设置该变量
    【解决方案2】:

    select_tag 只接受 3 个参数。看看它的definition。 所以保留为

    `select_tag 'city_id', options_from_collection_for_select(@cities, "id", "name", @states), {class: 'select2', multiple: true, 'data-placeholder' => 'Type City to Search'}`
    

    确保为您的 ajax 请求设置正确的格式(这应该是 jsonp

    $("#your_states_input_id").select2({
      placeholder: "Search for a city",
      ajax: { 
        url: "your_city_url_here",
        dataType: 'jsonp'
      }
    });   
    

    欲了解更多信息,请查看select2 ajax example

    【讨论】:

    • 我真的很喜欢有人对答案投了反对票并且没有说明他为什么这样做。
    • 你没有完整地阅读我的问题。我不能省略那个 {},在删除 dat {} 时它看起来就像一个下拉表。该输入字段将丢失。
    • 再次查看 select_tag 语法。它接受 3 个参数而不是 4 个。
    • 所以我没有办法实现这个吗? :(
    猜你喜欢
    • 2016-07-02
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多