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

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

问题描述

我在Ruby on Rails上使用Active Admin Gem。我有一个表单,我已经选择了类别和子类别,因此我必须填写数据。所以我在sqlite中创建了两个表,在活动的管理员资源中添加。



每件事情都可以正常工作,但子类别的下拉菜单根据选择的类别没有被过滤。



我是Ruby和RoR的新手。我不知道如何在选择类别后刷新子类别的下拉列表。



我知道我可以从AJAX和javascript中做到这一点,但我不知道在哪里编码另外,Active Admin中是否有特定的过滤器可以在没有ajax或javascript的情况下实现。



任何想法或帮助将被高度赞赏。

解决方案

我不知道是否有任何特定的过滤器可用于活动管理员,但是我以这种3步的方式解决了(假设类别 - 是一个房子,子类别 - 是一个单位):



第一步:定义包含ajax请求



(当然,你必须在routes.rb中预定义路径

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



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



 #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



第3步:渲染结果的过滤



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

  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
/ pre>

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.

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

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

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.

解决方案

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):

1-st step: define helper containing ajax request

(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

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

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

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

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