activeadmin 中的嵌套表单不保存更新 [英] Nested form in activeadmin not saving updates
问题描述
我在 ActiveAdmin 中有这些模型的嵌套表单(a :class_section has_many :class_dates):
I have a nested form in ActiveAdmin for these models (a :class_section has_many :class_dates):
class ClassDate < ActiveRecord::Base
belongs_to :class_section
validates :start_time, :presence => true
validates :end_time, :presence => true
end
和
class ClassSection < ActiveRecord::Base
belongs_to :class_course
has_many :class_dates
belongs_to :location
accepts_nested_attributes_for :class_dates
end
当我查看表格时,一切似乎都在正确的位置.但是,当我更新 class_date 时,它不会保存.
Everything seems to be in the right place when I look at the form. However, when I update a class_date, it doesn't save.
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id
form do |f|
f.inputs "Details" do
f.input :class_course, member_label: :id_num
f.input :min_students, label: "Minunum Students"
f.input :max_students, label: "Maxiumum Students"
f.input :location
end
f.inputs do
f.input :info, label: "Additional Information"
end
f.inputs "Dates" do
f.has_many :class_dates, heading: false do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
index do
column :class_course
column :location
default_actions
end
filter :class_course
filter :location
show do |cs|
attributes_table do
row :class_course do
cs.class_course.id_num + " - " + cs.class_course.name
end
row :location
row :min_students, label: "Minunum Students"
row :max_students, label: "Maxiumum Students"
row :info, label: "Additional Information"
end
panel "Dates" do
attributes_table_for class_section.class_dates do
rows :start_time, :end_time
end
end
active_admin_comments
end
end
这是 ClassDates 的 ActiveAdmin 文件:
Here is the ActiveAdmin file for ClassDates:
ActiveAdmin.register ClassDate, as: "Dates" do
permit_params :start_time, :end_time, :class_section_id
belongs_to :class_section
end
您能找出无法正确保存的原因吗?
Can you see a reason why it's not saving properly?
更新:我向 AA 添加了以下代码,现在它似乎可以工作了:
UPDATE: I added the following code to the AA and it seems to work now:
controller do
def permitted_params
params.permit!
end
end
如果有更好的解决方案,请告诉我.谢谢.
Let me know if there is a better solution. Thanks.
更新 2: 然而,有一个挥之不去的问题.我无法使用此表单删除 ClassDates.
UPDATE 2: There is one lingering problem however. I am unable to delete ClassDates using this form.
推荐答案
您需要允许嵌套参数,但您应该永远不要使用 params.permit!
.这是极其不安全的.试试这个:
You need to permit nested parameters, but you should never use params.permit!
. It's extremely unsafe. Try this:
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id,
class_dates_attributes: [ :id, :start_time, :end_time, :_destroy ]
form do |f|
# ...
f.inputs "Dates" do
f.has_many :class_dates, heading: false, allow_destroy: true do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
# ...
end
ClassDate
管理面板的配置(和 permitted_params
)与 ClassSection
管理面板中允许的参数无关.将它们视为应用内的独立控制器.
The configuration (and permitted_params
) of your ClassDate
admin panel has nothing to do with the permitted parameters within the ClassSection
admin panel. Treat them as separate controllers within the app.
将 allow_destroy: true
选项添加到 has_many
调用将向表单添加一个复选框,以允许您在提交表单时删除课程时间.
Adding the allow_destroy: true
option to the has_many
call will add a checkbox to the form to allow you to delete a class time upon form submission.
这篇关于activeadmin 中的嵌套表单不保存更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!