Форма за конкретно действие с ActiveAdmin

Как мога да създам формуляр за конкретно действие в контролер на ActiveAdmin? Нещо подобно на това:

= form_for @workbook, :url => {:controller => params[:controller], :action =>     "save_workbook_exercise"} do |f|
        = f.hidden_field :exercise_type, :value =>   @user_workbook_exercise.workbook_exercise.exercise_type, :class => "hidden_exercise_type"
        = f.hidden_field :data, :class => "hidden_svg_data"
        = f.hidden_field :user_id, value: @user.id
        = f.hidden_field :workbook_exercise_id, :value => @user_workbook_exercise.workbook_exercise.id, :class => "workbook_exercise_id"
        = f.submit "Save", :onclick => "saveSVG();", :class => "btn btn-primary", :id => "save_button"

Освен в синтаксиса "ActiveAdmin". Не съм използвал много ActiveAdmin и не можах да намеря това, което ми трябваше в документацията.


person cloudwalker    schedule 11.02.2014    source източник
comment
погледнете това gist.github.com/mikeatlas/5628317   -  person Rahul Singh    schedule 11.02.2014
comment
Хубаво! Благодаря! Това прави точно това, което исках.   -  person cloudwalker    schedule 12.02.2014


Отговори (2)


Ако формулярът се изобразява от същия контролер, който ще обработва заявката, можете да опростите формуляра.

Примерен ресурс за ActiveAdmin на работна книга:

ActiveAdmin.register Workbook do
  member_action :save_workbook_exercise, :method => :post do
    workbook = resource

    # update workbook using params[:workbook] ...

    workbook.save!
    redirect_to :action => :show
  end
end

И след това вашия шаблон на формуляр:

= semantic_form_for @workbook, :url => { :action => :save_workbook_exercise } do |f|
  = f.hidden_field :exercise_type, :value => @user_workbook_exercise.workbook_exercise.exercise_type, :class => "hidden_exercise_type"
  = f.hidden_field :data, :class => "hidden_svg_data"
  = f.hidden_field :user_id, value: @user.id
  = f.hidden_field :workbook_exercise_id, :value => @user_workbook_exercise.workbook_exercise.id, :class => "workbook_exercise_id"
  = f.actions do
    = f.action :submit, :label => "Save"
person Charles Maresh    schedule 13.02.2014
comment
Това ми помогна. Благодаря :) - person ARK; 26.08.2020
comment
О, това отново ми помогна да дефинирам конкретните си действия. Загубих се в синтаксиса :P - person ARK; 26.08.2020

Работя с Rails 5 и се сблъсках с подобна ситуация, при която трябва да персонализирам атрибута за действие на формата. И така, най-добрият начин, който намерих да постигна това, беше чрез създаване на частична форма в папката на съответния изглед.

Първо кажете на вашия ресурс, че ще използвате персонализиран формуляр, така че добавете този ред към вашия ресурсен файл:

# app/admin/organizations.rb

form partial: "form"

Сега можете да създадете вашия частичен, като използвате Arbre Components, като този пример:

# app/views/admin/organizations/_form.html.arb

active_admin_form_for [:admin, resource] do |f|
  tabs do
    tab 'General Configuration' do
      f.inputs 'Organization Details' do
        admin_accounts = User.with_role(:admin).order('email ASC')
        site_collection = Site.where("subdomain <> ''").map { |site| ["#{site.subdomain}", site.id ]}
        f.input :name
        f.input :kind, :as => :enum
        f.input :carereceiver_kind, :as => :enum
        f.input :account_manager, :as => :select ,:collection => admin_accounts
        f.input :site_id, as: :select ,collection: site_collection
        f.input :office_phone
        f.input :office_fax
        f.input :office_address
        f.input :company_logo, :as => :file
        f.input :letterhead
        f.input :base_url, label: 'BASE URL'
        f.input :dynamics_url, label: 'DYNAMICS URL'
        f.input :genoa_site_id, label: 'GENOA SITE ID'
      end

      f.inputs 'Organization Settings' do
        f.input :demo_account
        f.input :appointment_not_started_notifications_enabled
        f.input :erx_enabled
        f.input :patients_can_book_appointments
        f.input :new_providers_can_manage_their_own_availability_by_default
        f.input :clarity_enabled
        f.input :whitelist_enabled
        f.input :bed_form_enabled
        f.input :patient_email_required, label: 'Require patient email addresses'
        f.input :patient_credit_card_required, label: 'Require patient credit card information'
        f.input :enable_patient_survey, label: 'Enable patient survey'
        f.input :enable_provider_survey, label: 'Enable provider survey'
        f.input :rcopia4_enabled, label: 'Rcopia 4 enabled'
        f.input :share_notes_across_providers_enabled
        f.input :d2c_mode
        f.input :sso_login_only
        f.input :allow_invited_patients_to_complete_profile
        f.input :allow_overlapping_appointments, as: :select
        f.input :media_mode, :as => :enum
      end

      f.inputs('Organization Contacts', { class: 'organization_contacts_section' }) do
        saved_contacts = f.object.organization_contacts.count
        n = 5 - saved_contacts.to_i
        n.times do
          f.object.organization_contacts.build
        end
        contact_counter = 0
        f.fields_for :organization_contacts do |m|
          contact_counter = contact_counter +  1
          m.inputs "Contact #{contact_counter}" do
            m.input :name, placeholder: 'Name'
            m.input :title, placeholder: 'Title'
            m.input :email, placeholder: 'Email'
            m.input :phone_number, placeholder: 'Phone Number'
          end
        end
      end
    end

    tab 'Appointment Parser Configuration' do
      f.inputs 'Appointment Parser Configuration' do
        f.input :appointment_parsers, as: :select, input_html: { multiple: true }
      end
    end

    tab 'EMR Settings' do
      f.inputs 'Settings' do
        f.input :emr_integrated, label: 'Enable EMR integration'
        f.input :emr_processor_type, as: :select, collection: Organization::AVAILABLE_EMR_PROCESSORS
        f.input :send_to_emr, label: 'Enable integration from 1DW to EMR'
        f.input :receive_from_emr, label: 'Enable integration from EMR to 1DW'
      end
    end

    tab 'Athena EMR Settings' do
      f.inputs 'Settings' do
        f.input :athena_practice_id, label: 'Athena Practice ID', input_html: { disabled: !f.object.has_athena_processor? }
        f.input :athena_department_number, label: 'Athena Department ID', input_html: { disabled: !f.object.has_athena_processor? }
      end
    end
  end

  f.actions
end

Както можете да видите с admin_organization_path можете да посочите URL адреса към всеки друг, който искате, но също така можете да персонализирате method за изпращане на формуляра.

Също така се уверете, че използвате resource за блока active_admin_form_for, защото ще получите грешка, ако се опитате да използвате нещо като @organization.

Формулярът съдържа възможен начин за добавяне на вложени ресурси към основния всеки път, когато сте конфигурирали съответните моделни връзки.

И с това трябва да работи добре.

Надявам се да е полезно за някой друг.

person alexventuraio    schedule 14.05.2018