Active Admin嵌套选择

时间:2016-03-30 12:02:37

标签: ruby-on-rails ruby activeadmin

我的ruby on rails项目有Active Admin gem。我想要做的是创建一个产品并有2个选择框(1-Category,2-Department),其中第二个根据第一个改变它的内容。经过无数个小时的搜索,我仍然无法让它发挥作用。

class Product < ActiveRecord::Base
belongs_to :category, dependent: :destroy, counter_cache: true
has_one :department, through: :category
end

class Category < ActiveRecord::Base
belongs_to :department, counter_cache: true
has_many :products
end

class Department < ActiveRecord::Base
end

我之所以这样做,是因为某些部门可能会有相同的类别名称,而且会将产品添加到错误的部门。

我试过question 9579402,但我如何理解他的问题是他只有2个模特而且他正在从所选类别创建子类别

这是一个熟悉的事情,但他使用get ajax request Git/Dglgmut/6328501

尝试question 9579402让我犯错:

Started POST "/admin/products/change_categories" for ::1 at 2016-03-30 14:51:09 +0300
ActionController::RoutingError (No route matches [POST]    "/admin/products/change_categories"):

这就是我在routes.rb中所拥有的。

routes.rb = post 'change_categories'        =>'products#change_categories'  
http://localhost:3000/rails/info/routes = change_categories_path    POST    /change_categories(.:format)    products#change_categories 

我想这是因为我可以使用Active Actions for Active admin,所以我试了一下

member_action :change_categories, :method => :get do
  @categories = Department.find_by_id(params[:department_id]).try(:categories)
  render :text=>view_context.options_from_collection_for_select(@categories, :id, :category_number)
end

但是得到了和以前一样的错误。任何帮助将不胜感激,谢谢。

更新: 我是初学者,但如果我是正确的,change_categories应该在类别控制器中,因为它在产品控制器中搜索该方法,

POST "/admin/products/change_categories"

所以我将类别资源添加到Active Admin并将该方法添加到类别控制器,但是现在有办法使用该控制器吗?类似的东西:

f.input :department, :input_html => { :onchange => remote_request(controller => "categories", :post, :change_categories, {:department_id=>"$('#department_id').val()"}, :category_id) }

2 个答案:

答案 0 :(得分:0)

问题是你的member_action方法是&#34;得到&#34;并且您要发布到控制器。改变&#34; POST&#34;做一个Get请求应该解决问题。

答案 1 :(得分:0)

因此,经过无数个小时,我设法让这个工作。我认为问题出现在旧版本中,或者我采用代码的模型之间存在差异。因此,在Active Admin而不是member_action中,我使用了在路由中更有意义的collection_action,因为 member_action创建路由/admin/products/:id/change_categories(.:format) 干扰show动作并使用它而不是change_categories动作。

所以heres collection_action:

collection_action :change_categories, :method => :get do
@categories = Category.where("department_id = ?", Department.find(params[:product_department_id]))
render :text => view_context.options_from_collection_for_select(@categories, :id, :name)
end

这会生成正确的路径admin / products / change_categories,现在我可以传递params [:product_departmemnt_id],这是从选择框中选择的id。

这是从以下地址获取Id的地方:

f.input :description
    f.input :department, prompt: "Select department", :input_html => {
    onchange: remote_get("change_categories", 'product_department_id', :product_category_id)
}
    f.input :category

对于一些ajax初学者的提示,请务必检查网页的源代码,看看你是否使用了正确的id或类或其他什么,这给我带来了麻烦。

您想要添加的是辅助方法,有两种方法可以在application_helper.rb或app / helpers中执行此操作。如果将其添加到appication_helper.rb,则必须将其包含在config / initializers / active_admin.rb初始化程序中:

ActiveAdmin.setup do |config|
  ....
  some config
  ....
end

module ActiveAdmin::ViewHelpers
  include ApplicationHelper
end

但是这可以访问我认为不需要的所有辅助方法,或者可能/可能不会减慢它,所以我在app / helpers中创建了名为“active_admin”的新文件夹并创建了一个文件“form_helper” .rb“这段代码来自Dglgmut/6328501 git

def remote_get(path, member,target_tag_id)
 "$.get('#{path}/?#{member}=' + $('##{member}').val(),
  function(data) {$('##{target_tag_id}').html(data);}
 );"
end

不太了解js,但我认为这会使得对admin / products / change_categories /?product_department_id = 1的请求,其余的发生在helper方法中。如果有人发现这一点,我希望这可以帮助您并获得您需要的信息。我也建议你所谓的“橡皮鸭”调试,没有笑话,这有时会有所帮助。

相关问题