本文介绍了在ActiveAdmin中创建动态表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我非常想在活跃的管理员中构建自定义表单。我已经弄清楚了,甚至使用了活跃的管理员来设置自己的自定义表单。但是,我需要在这里创建一个动态表单。这涉及进行一些ajax调用并返回表单的部分内容供用户填写。

Hi guys I'm pretty stuck with building a customised form in active admin. I've figured out and even set up my own customised forms using active admin. However I need to create a dynamic form here. This involves making some ajax calls and returning partials of a form for the user to fill out.

似乎您可以创建一个成员操作,但是该操作基于已创建的资源。就我而言,我需要在尚未创建的资源上创建多个条目。

It seems that you can create a member action but that is on resources that have been created. In my case I need to create multiple entries on a resource that is yet to be created.

推荐答案

这可能会让您入门。一种方法是将ajax路由添加到config.routes(只是一些随机示例代码):

This might get you started. A way could be to add your ajax route to config.routes (just some random example code):

match '/admin/search/authors' =>  'admin/articles#search_authors',  :via => 'post', :as => :admin_search_authors

然后在正确的寄存器块中,在这种情况下,位于app / admin / articles.rb中可以把您的控制器逻辑

Then in the correct register block, in this case in app/admin/articles.rb you can put your controller logic

ActiveAdmin.register Article do
  ...
  controller do
    def search_authors
      @authors = Author.find_by_query params[:query]
      render :layout => false, :template => 'admin/authors/search'
    end
  end

end

在这种情况下,模板仅呈现要放入表单的无序列表。位于app / views / admin / authors / search.html.erb,看起来像:

In this case the template just renders an unordered list to be put in the form. Located at app/views/admin/authors/search.html.erb and looks like:

<ul>
  <% if @authors.count < 1 %>
    <li>None</li>
  <% else %>
    <% @authors.each do |a| %>
      <li>
      <a href="#" onclick="javascript:insert_your_own_voodoo_function_here();return false;">
        <%= raw a.listname %>
      </a>
      </li>
    <% end %>
  <% end %>
</ul>

您的自定义jquery触发ajax调用可能看起来像这样,这里使用了delayObserver(感谢到 ):

Your custom jquery to trigger the ajax-call could look like this, here a delayedObserver is used (thanks to http://code.google.com/p/jquery-utils/wiki/DelayedObserver ):

$(function() {
     $('#search_author').delayedObserver(function() {
       $('#search_author').after('<img class="ajax-loader" id="ajax-loader" alt="Ajax-loader" src="/images/ajax-loader.gif">');
       $.ajax({
         url: '<%= admin_search_authors_path %>',
         dataType: 'html',
         type: 'post',
         data: { query: $('#search_author').val(), authenticity_token: $('meta[name=csrf-token]').attr("content")},
         success: function(data) {
           $('.ajax-loader').remove();
           $('#chooseauthor').html(data);
           $("#artikel_edit_auteurs").effect("highlight", {}, 1500);
         }
       });
     }, 0.5);
   });

一切皆有可能。只需分析一下您真正需要的是:表单局部,嵌套表单局部?取决于选择?也许您想构建一个多步骤表单。祝你好运。

Anything is possible. Just analyze what you really need: form partials, nested form partials? Depending selects? Maybe you want to build a multiple step form instead. Good luck.

这篇关于在ActiveAdmin中创建动态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:52