我可以从 activeadmin 表单提交覆盖模型中的 to_param
Can I override to_param in model from activeadmin form submit
我继承了一个使用 Active Admin 创建的系统。我需要将其中一个模型 "report" 添加到 Active Admin 中。我这样做了,检查了仪表盘中的索引显示,一切看起来都很好。但是,当我尝试使用任何 CRUD 功能时,它会中断并返回错误:
Couldn't find Report with id=name_of_report
问题是 Active Admin 正在生成这样的路由(其中 report_title 是 report.title 值):
http://localhost:3000/admin/reports/report_title
而不是:
http://localhost:3000/admin/reports/1
查看模型表明问题与具有 to_param 方法的报表模型有关:
def to_param
title
end
如果我删除它,那么我的活动管理页面就会开始正常工作。但是,如果我删除 to_param,已经编写了大量报告功能,这些功能会中断,因为它需要 URL 的格式如上文所述 (http://localhost:3000/admin/reports/)。
我已经使用 "link_to" 创建了自己的 link 以进行添加、查看、删除(见下文)。我也有新的 link 以类似的方式工作。
app/admin/reports.rb
index do
selectable_column
column :title
column :id
column :column_method
column :row_method
actions defaults: false do |action|
link_to("View", admin_report_path(action.id), { :class=> "member_link view_link" } ) +
link_to("Edit", edit_admin_report_path(action.id), { :class=> "member_link view_link" } ) +
link_to("Delete", admin_report_path(action.id), { :class=> "member_link view_link" }, method: :delete, data: { confirm: 'Are you sure?' })
end
end
问题是我无法让表单在提交时更新或创建表单的详细信息。
我想知道是否还有其他人遇到过这个问题并且知道解决它的方法?
非常感谢您提出任何建议。
编辑
Timo 的解决方案对我来说效果很好,非常感谢您的快速响应。对于那些感兴趣的人,我的 report.rb 程序现在看起来像这样:
ActiveAdmin.register Report do
controller do
resources_configuration[:self][:finder] = :find_by_title
end
index do
selectable_column
column :title
column :id
column :column_method
column :row_method
actions
end
form do |f|
f.inputs 'Details' do
f.input :title
f.input :column_method
f.input :row_method
f.input :id # This should probably be hidden.....
f.buttons
end
end
end
ActiveAdmin.register Report do
controller do
resources_configuration[:self][:finder] = :find_by_title
end
end
我继承了一个使用 Active Admin 创建的系统。我需要将其中一个模型 "report" 添加到 Active Admin 中。我这样做了,检查了仪表盘中的索引显示,一切看起来都很好。但是,当我尝试使用任何 CRUD 功能时,它会中断并返回错误:
Couldn't find Report with id=name_of_report
问题是 Active Admin 正在生成这样的路由(其中 report_title 是 report.title 值):
http://localhost:3000/admin/reports/report_title
而不是:
http://localhost:3000/admin/reports/1
查看模型表明问题与具有 to_param 方法的报表模型有关:
def to_param
title
end
如果我删除它,那么我的活动管理页面就会开始正常工作。但是,如果我删除 to_param,已经编写了大量报告功能,这些功能会中断,因为它需要 URL 的格式如上文所述 (http://localhost:3000/admin/reports/)。
我已经使用 "link_to" 创建了自己的 link 以进行添加、查看、删除(见下文)。我也有新的 link 以类似的方式工作。
app/admin/reports.rb
index do
selectable_column
column :title
column :id
column :column_method
column :row_method
actions defaults: false do |action|
link_to("View", admin_report_path(action.id), { :class=> "member_link view_link" } ) +
link_to("Edit", edit_admin_report_path(action.id), { :class=> "member_link view_link" } ) +
link_to("Delete", admin_report_path(action.id), { :class=> "member_link view_link" }, method: :delete, data: { confirm: 'Are you sure?' })
end
end
问题是我无法让表单在提交时更新或创建表单的详细信息。
我想知道是否还有其他人遇到过这个问题并且知道解决它的方法?
非常感谢您提出任何建议。
编辑
Timo 的解决方案对我来说效果很好,非常感谢您的快速响应。对于那些感兴趣的人,我的 report.rb 程序现在看起来像这样:
ActiveAdmin.register Report do
controller do
resources_configuration[:self][:finder] = :find_by_title
end
index do
selectable_column
column :title
column :id
column :column_method
column :row_method
actions
end
form do |f|
f.inputs 'Details' do
f.input :title
f.input :column_method
f.input :row_method
f.input :id # This should probably be hidden.....
f.buttons
end
end
end
ActiveAdmin.register Report do
controller do
resources_configuration[:self][:finder] = :find_by_title
end
end