根据第一个下拉菜单

根据第一个下拉菜单

本文介绍了根据第一个下拉菜单(Rails 3.2)的选择,启动管理员 - 第二个下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我无法在主动管理员中执行一个表单,第二个下拉菜单(子类别)必须在其前面的下拉列表中进行调整。 >基本上我想先为一家公司设立一个行业,然后选择分行业下降。我已经创建了两个模型:




  • CompanySector

  • CompanySubsector



由于实际原因,我在代码中使用sector和subsectors这个词。



我是试图在,但没有成功。



1-我创建路线



#routes.rb

 资源:公司

post'/ change_company_subsectors'=>'company_sectors#change_company_subsectors'

2-我在Helpers / ApplicationHelper.rb中定义了一个包含ajax请求的定制助手

  def remote_request(type,path,params = {},target_tag_id)
$。#{type }('#{path}',
{#{params.collect {| p | #{p [0]}:#{p [1]}} .join(,)}},
函数(数据){$('## {target_tag_id}')数据);}
);
end

我放在admin / company.rb这个方法:onchange操作

  controller do 
def change_company_subsectors
@company_subsectors = CompanySector.find_by_id(params [:company_sector_id])。try(:company_subsectors)
render:text => view_context.options_from_collection_for_select(@company_subsectors,:id)
end
end

4-在我活跃的amdin表单admin / company.rb中,我在编辑表单中添加:

  form do | f | 

f。输入companydo

f.input:company_sector_id,:input_html => {
:onchange => remote_request(:post,:change_company_subsectors,{:company_sector_id =>$ ('#company_sector_id')。val()},:company_subsector_id)
}
f.input:company_subsector_id
end

但是结果是非常糟糕的:我甚至没有看到一个下降的人口与部门和分部门的可能性。



我应该看到的部门:休闲/旅游/时尚...但我什么也看不见。
我也看不到SubSectors。



我不应该使用formtastic集合,如::as => :select,:collection =>有没有人知道我犯了哪些错误?



>解决方案

首先,我建议不要将JS直接放在HTML元素上;以后我们经常会做这些事情:

 

code> select1 = fieldset.find'select:first'#Companies
select2 = fieldset.find'select:last'#Users
select1.change( - >
$。 get'/admin/users.json',q:{company_id_eq:$(@)。val()},(data) - >
select2.html data.map(u) - & < option value =#{u.id}>#{u.name}< / option>
).change()
/ pre>

I'm having trouble implementing a form on active admin where the second dropdown (subcategory) has to adjust to the category choice on the drop down that precedes it.

Basically I want to first set a industry for a company then select among sub-industry drop down. I have created 2 models:

  • CompanySector
  • CompanySubsector

For practical reasons, I use the words 'sector' and 'subsectors' in the code.

I am trying to use @okliv suggestions on Active Admin - refresh second drop down based on first drop down, Ruby on Rails but with no success.

1- I created a route

In #routes.rb

resources :companies

  post '/change_company_subsectors' =>'company_sectors#change_company_subsectors'

2- I defined a define helper containing ajax requestin Helpers/ApplicationHelper.rb

    def remote_request(type, path, params={}, target_tag_id)
       "$.#{type}('#{path}',
             {#{params.collect { |p| "#{p[0]}: #{p[1]}" }.join(", ")}},
             function(data) {$('##{target_tag_id}').html(data);}
    );"
    end

3- i put in admin/company.rb this method for :onchange action

    controller do
    def change_company_subsectors
      @company_subsectors = CompanySector.find_by_id(params[:company_sector_id]).try(:company_subsectors)
      render :text=>view_context.options_from_collection_for_select(@company_subsectors, :id)
      end
    end

4- And in my active amdin form admin/company.rb, i added this in the edit form:

form do |f|

f.inputs "company" do

  f.input :company_sector_id, :input_html => {
    :onchange => remote_request(:post, :change_company_subsectors, {:company_sector_id => "$('#company_sector_id').val()"}, :company_subsector_id)
  }
  f.input :company_subsector_id
end

But the results is very bad: I don't even see a dropdown populated with the Sectors and Subsectors possibilities.

I should see for Sectors: Leisure/Travel/Fashion... but I see nothing.And i see also nothing for SubSectors.

Shouldn't I use formtastic Collections like: :as => :select, :collection => CompanySubsector.all?

Does anyone know where I have made mistakes?

解决方案

First off I'd suggest against putting JS directly on an HTML element; it makes for much more difficult debugging later on.

Here's what I often do:

select1 = fieldset.find 'select:first' # Companies
select2 = fieldset.find 'select:last'  # Users
select1.change(->
  $.get '/admin/users.json', q: {company_id_eq: $(@).val()}, (data)->
    select2.html data.map (u)-> """<option value="#{u.id}">#{u.name}</option>"""
).change()

这篇关于根据第一个下拉菜单(Rails 3.2)的选择,启动管理员 - 第二个下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:19