【问题标题】:Active Admin nested select活动管理员嵌套选择
【发布时间】: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" =&gt; "products#change_categories" 可以完成您的工作

标签: ruby-on-rails ruby activeadmin


【解决方案1】:

问题是您的 member_action 方法是“get”并且您正在发布到控制器。从“POST”更改为执行 Get 请求应该可以解决问题。

【讨论】:

  • 这是它现在吐出的内容:在 2016-03-30 15:28:56 +0300 由 Admin::ProductsController# 开始为 ::1 获取“/admin/products/change_categories”显示为 / 参数:{"id"=>"change_categories"} 产品负载 (0.7ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 0]] Completed 404 Not Found in 12ms (ActiveRecord: 1.1ms) ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=change_categories):
  • 从 routes.rb 中删除帖子“change_categories”。 member_action 将添加 active_admin 路由。如果没有,则在 routes.rb 底部附近添加 ActiveAdmin.routes(self)。
  • 粘贴您的routes.rb 文件以粘贴并分享链接。
  • 删除第 18 行。
  • 好吧,从 routes.rb 中删除 post 'change_categories' 按照你说的做了它必须做的事情,但是错误,cmets 中的那个仍然存在
【解决方案2】:

因此,经过无数小时后,我设法让它发挥作用。我认为问题在于旧版本或我获取代码的模型之间的差异。因此,在 Active Admin 而不是 member_action 中,我使用了在路由中更有意义的 collection_action,因为 member_action 创建路由 /admin/products/:id/change_categories(.:format) 干扰显示操作并使用它而不是 change_categories 操作。

这里是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 ,我现在可以在其中传递从选择框中选择的 id 的 params[:product_departmemnt_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或class之类的,这让我很头疼。

接下来要添加的是 helper 方法,有两种方法可以做到这一点,在 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 发出 get 请求,其余的发生在辅助方法中。如果有人偶然发现这一点,我希望这对您有所帮助并提供您需要的信息。我还建议您进行所谓的“橡皮鸭”调试,不要开玩笑,这有时会有所帮助。

【讨论】:

  • PS:添加或编辑(我认为)辅助方法后,活动管理员需要重新启动服务器。
猜你喜欢
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多