响应做格式问题

Nidhin SG

我有2个字段,例如州和城市。应根据相应的州来加载城市。而且我的领域并非纯粹是DropDown。它们就像具有自动完成功能的输入字段,使我们可以选择多个选项(请参阅图片。)

屏幕截图1

屏幕截图2

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

<%= 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页的{}表单中删除它,它将可以正常工作。但是,我得到的只是一个简单的下拉列表,在我的第一张图片中没有像阿拉巴马州加利福尼亚这样的输入字段。有什么办法可以解决这个问题。

理查德·派克

我认为您的问题与呈现表单的方式有关,而不是与respond_toRails中块有关。我看到您正在使用select2


select_tag

我认为您select_tag的电话没有正确打电话。这与您是否content-typerespond_to

这看起来似乎很明显,但是事实是您是在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的问题)


选择

正如章鱼保罗所提到的,您对的调用select_tag基本上意味着您仅需要使用3参数,而f.select具有4个参数:

select_tag(name, option_tags = nil, options = {})

select(object, method, choices, options = {}, html_options = {})

表面修复将使用form_builder带有新select标签对象,而不是裸对象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);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章