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.