【发布时间】:2016-07-18 10:12:03
【问题描述】:
我的 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请求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 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) }
【问题讨论】:
-
我认为添加
get "/admin/products/change_categories" => "products#change_categories"可以完成您的工作
标签: ruby-on-rails ruby activeadmin