Activeadmin extension: Cascade selection _ror

Source: Internet
Author: User
Activeadmin extension: Cascade selection


1. Create a new Common.js file in Active_admin.js's sibling directory,
And then join in the active_admin.js.
= Require common
(If the new file and Active_admin.js are not in the same sibling directory, the file path after require should be modified accordingly)


2, Common.js the relevant code is as follows:


The article_title here is automatically generated for form forms, in the form "[Model]_[field]", which is modified according to the actual situation.

    $ (function () {
      $ (' #article_title '). Change (function () {
        $.ajax ({
          URL: '/admin/articles/get_secondary_ Subject_by_subject ',
          type: ' POST ',
          data: {
            subject_id: $ (this). val ()
          },
          dataType: ' JSON ',
          Headers: {
            ' X-csrf-token ': $ (' meta[name= ' csrf-token '] '). attr (' content ')
          },
          success:function (data) {
            $ (' #article_content '). HTML ("");
            var str = ';
            for (var i = 0; i < data.length ++i) {
              str = ' <option value= ' + data[i].id + ' "> ' + data[i].name + ' </ Option> ';
            };
            $ (' #article_content '). Append (str);
          },
          error:function () {

          }
        });
      }
    );

3, the RB file related code is as follows:

   Collection_action:get_secondary_subject_by_subject,: Method =>:p ost do
     @subject_id = params[:subject_id]
     @secondary_subjects = Subject.where ("parent_subject_id =?", @subject_id)
     Render:json => @secondary_ Subjects.to_json (: Only => [: ID,: name]) ' and return '

   Controller do
     def new
       @article = article.new< c8/> @subjects = Subject.order ("id ASC"). Where ("parent_subject_id is null")
       @secondary_subjects = Subject.order (" ID ASC "). WHERE (" parent_subject_id =? ", @subjects. First.id)
     end

     def edit
       @subjects = Subject.order (" id ASC "). Where (" parent_subject_id is null ")
       @secondary_subjects = Subject.order (" id ASC "). WHERE (" parent_subject_ id =? ", @subjects. First.id)
     end

   f.input:title,: As =>: SELECT,: Collection => subjects,: inclu  De_blank => false
   f.input:content: As =>: SELECT,: Collection => secondary_subjects,: Include_blank => False


The above content is sorted by Bruce.xue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.