简体   繁体   English

在rails activeadmin中渲染表格部分,而不使用表格形式

[英]render form partials in rails activeadmin without using formtastic forms

My clubs.rb file is given below: 我的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

Please help me how to use the partial so that i can use the same partial in the dashboard of activeadmin. 请帮助我如何使用部分,以便我可以在activeadmin的仪表板中使用相同的部分。 Here in the above code I have written the form for creating new clubs in the active admin and the show code is for displaying the details of the clubs. 在上面的代码中,我已经在活动管理员中编写了用于创建新俱乐部的表单,并且显示代码用于显示俱乐部的详细信息。

My suggestion is to use a partial with the .html.arb extension and the active_admin_form_for helper. 我的建议是使用带有.html.arb扩展名和active_admin_form_for辅助.html.arb的局部.html.arb

You can put your form in a partial. 您可以将表单分为部分表单。 You can even make your partial .arb instead of .erb . 您甚至可以将部分.arb代替.erb The .arb extension allows you to write with the ActiveAdmin DSL in the partial. .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

Then, in your dashboard, or wherever else you want to render the form you can use 然后,在您的信息中心或其他任何要呈现表单的地方,都可以使用

# app/admin/dashboard.rb
render partial: 'clubs/form', locals: { resource: Club.new }

# app/admin/clubs.rb
form partial: 'form'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM