Answer the question
In order to leave comments, you need to log in
How to update values in collection_select with another collection_select?
There is:
quizzes.coffee
# $(document).on 'page:change', ->
$(document).on 'ready page:load', ->
# $('#subcategories_select').hide()
$('form').on 'change', ->
# if $('#category_id').val()
if true
$.ajax
url: 'update_subcategory'
data: category_id: $('#category_id').val()
dataType: 'script'
$('#subcategories_select').show()
else
$('#subcategories_select').hide()
class QuizzesController < ApplicationController
def new
@quiz = Quiz.new
@notcategory = Category.where "parent_id = ?", params[:category_id]
end
def update_subcategory
if params[:category_id]
# @notcategory = Category.all.collect { |c| [c.name, c.id] }
@notcategory = Category.all.map { |c| [c.name, c.id] }
else
@notcategory = Category.all.map { |c| [c.name, c.id] }
end
render nothing: true, content_type: 'text/html', status: 200
end
private
def quiz_params
params.require(:quiz).permit(:title, :image, :description, :no_questions, :category_id)
end
end
= form_for @quiz, remote: true do |f|
.field
= f.label :title
= f.text_field :title
.field
= f.label :image
= f.text_field :image
.field
= f.label :category
= f.collection_select :category_id, Category.where(parent_id: nil), :id, :name, {prompt: "Choose a Category"}, id: "category_id"
#subcategories_select
= f.label :subcategory
= f.collection_select :category_id, @notcategory, :id, :name, id: "subcategory_id"
/ - if params[:category_id]
/ = f.collection_select :category_id, Category.where(parent_id: params[:category_id]), :id, :name, { prompt: 'Choose a subcategory' }, id: "subcategory_id"
.field
= f.label :description
= f.text_field :description
.field
= f.label :number_of_questions
= f.number_field :no_questions
.actions
= f.submit 'Save'
Answer the question
In order to leave comments, you need to log in
To start
url: 'update_subcategory'
#replace
url: '/url'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question