使用ActiveAdmin进行特定操作的表单

时间:2014-02-11 03:29:10

标签: ruby-on-rails 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,也无法在文档中找到我需要的内容。

2 个答案:

答案 0 :(得分:1)

如果表单由处理请求的同一个控制器呈现,则可以简化表单。

示例Workbook 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"

答案 1 :(得分:0)

我正在使用 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中看到的那样,您可以将网址指向您想要的任何其他网址,同时您也可以自定义method以发送表单。

  

另外,请确保对resource块使用active_admin_form_for,因为如果您尝试使用@organization之类的内容,则会出现错误。

     

每次配置相应的模型关系时,表单都包含将嵌套资源添加到主体资源的可能方法。

有了这个,它应该可以正常工作。

我希望它对其他人有用。