我的clubs.rb文件如下所示:
ActiveAdmin.register Club do
permit_params :name, :email, :admin_id, club_profile_attributes: [:id, :club_id, :address_1, :address_2, :city, :state, :country_id, :lat, :long, :website, :email, :fiscal_number, :phone_number_1, :phone_number_2]
before_save do |club|
if user_id = User.find_by_email(params[:club][:email]).try(:id)
club.admin_id = user_id
else
user = User.set_email params[:club][:email]
club.admin_id = user.id
end
club.id = club.club_profile.club_id
end
form do |f|
f.inputs "Details" do
f.semantic_errors *f.object.errors.keys
#club name
f.input :name, label: 'Club Name'
#search for user email field
f.input :email, label: 'Club Admin Email', input_html: {value:
f.object.admin.present? ? f.object.admin.email : ''}
#club profile and country for form
f.inputs for: [:club_profile, f.object.club_profile || ClubProfile.new] do |ff|
ff.input :address_1
ff.input :address_2
ff.input :city
ff.input :state
ff.input :country_id, as: :select, collection: Country.all.collect {|country| [ country.name, country.id]}, include_blank: false
ff.input :lat
ff.input :long
ff.input :website
#Club email
ff.input :email, label: 'Club Email'
ff.input :fiscal_number
ff.input :phone_number_1
ff.input :phone_number_2
end
f.actions
end
end
show do
attributes_table do
row "Club Admin Email" do
club.admin.email
end
attributes_table_for club.club_profile do
row :address_1
row :address_2
row :city
row :state
row "Country" do
club.club_profile.country.name
end
row :lat
row :long
row :website
row "Club Email" do
club.club_profile.email
end
row :fiscal_number
row :phone_number_1
row :phone_number_2
end
end
end
end
请帮助我如何使用分部,以便我可以在activeadmin的面板中使用相同的分部。在上面的代码中,我已经在活动管理员中编写了创建新俱乐部的表格,显示代码用于显示俱乐部的详细信息。
我的建议是使用带有.html.arb
扩展名和active_admin_form_for
助手的分部。
你可以把你的表格放在分部中。您甚至可以使您的部分.arb
而不是.erb
。.arb
扩展允许您在部分中使用ActiveAdmin DSL进行编写。
# app/views/clubs/_form.html.arb
active_admin_form_for resource do |f|
f.inputs "Details" do
f.semantic_errors *f.object.errors.keys
#club name
f.input :name, label: 'Club Name'
end
# etc
f.actions
end
然后,在你的仪表板中,或者在你想呈现表单的其他地方,你可以使用
# app/admin/dashboard.rb
render partial: 'clubs/form', locals: { resource: Club.new }
# app/admin/clubs.rb
form partial: 'form'