Active Admin - 基于第一个下拉列表刷新第二个下拉列表,Ruby on Rails [英] Active Admin - refresh second drop down based on first drop down, Ruby on Rails

查看:34
本文介绍了Active Admin - 基于第一个下拉列表刷新第二个下拉列表,Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ruby on Rails 上使用 Active Admin Gem.我有一个表格,我在其中选择了类别和子类别,然后相应地我必须填写数据.所以我在活动管理资源中添加的 sqlite 中创建了两个表.

I am using Active Admin Gem on Ruby on Rails. I have a form in which i have selected category and sub category and then accordingly i have to fill the data. So i created two tables in sqlite added in active admin resouce.

一切正常,但子类别的下拉菜单并未根据所选类别进行过滤.

Every thing is working fine but the drop down of sub category is not getting filtered based on the category choosen.

我也是 Ruby 和 RoR 的新手.选择类别后,我不知道如何刷新子类别的下拉菜单.

I am new to Ruby and RoR too. I don't know how to refresh dropdown of the subcategory after selecting category.

我知道我可以通过 AJAX 和 javascript 来实现,但我不知道在哪里编写代码?

I know i can do it from AJAX and javascript but i dont know where to code for that?

此外,Active Admin 中是否有任何特定的过滤器可以在没有 ajax 或 javascript 的情况下实现.

Also, is there any specific filter avaliable in Active Admin which will make it happen without ajax or javascript.

任何想法或帮助将不胜感激.

Any ideas or help will be highly appreciated.

推荐答案

我不知道 Active Admin 中是否有任何特定的过滤器可用,但我通过这 3 个步骤的方式解决了它(假设类别 - 是房子, subcategory - 是一个平面):

i don't know if there is any specific filter avaliable in Active Admin, but i solved it in this 3-steps way (assuming category - is a house, subcategory - is a flat):

(当然,你必须在routes.rb中预先定义path)

(of course, you have to predefine path in routes.rb)

#application_helper.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

第二步:为:onchange动作添加这个方法

2-nd step: add this method for :onchange action

#admin/inhabitants.rb (DSL with formtastic)

form do |f|
  f.inputs do
  #...
    f.input :house, :input_html => {
        :onchange => remote_request(:post, :change_flats, {:house_id=>"$('#house_id').val()"}, :flat_id)
    }
    f.input :flat
  #...
  end
end

第三步:渲染过滤结果

(你可以渲染部分而不是 :text,我决定把它放在一个 activeadmin 资源文件中)

3-rd step: render result of filtering

(you can render partial instead of :text, I decided leave it in one activeadmin resource file )

controller do
  def change_flats
    @flats = House.find_by_id(params[:house_id]).try(:flats)
    render :text=>view_context.options_from_collection_for_select(@flats, :id, :flat_number)
  end
end

这篇关于Active Admin - 基于第一个下拉列表刷新第二个下拉列表,Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆