简体   繁体   中英

render form partials in rails activeadmin without using formtastic forms

My clubs.rb file is given below:

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. 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.

You can put your form in a partial. You can even make your partial .arb instead of .erb . The .arb extension allows you to write with the ActiveAdmin DSL in the partial.

# 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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