Y
Y
Yauheni Dakuka2015-08-11 15:42:28
JavaScript
Yauheni Dakuka, 2015-08-11 15:42:28

How to update values ​​in collection_select with another collection_select?

There is:
d3262dfd63e245e7980575d6b9cd8069.png

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()


controllers/quizzes_controller.rb
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


quizzes/_form.html.haml
= 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'


Essence: there are two collection_select. When one value is selected, the other displays one list of values, and when a different value displays another list of values.
Question: How can I pass new values ​​from the controller from the update_subcategory method to the second collection_select? So far I'm only getting the params[:category_id] value in the update_subcategory method from the first collection_select.

Googled similar situations, but nothing helped.
Here is a google search result:
How to use collection_select and select helpers in...
How to pass selected value from collection select?
Dynamic Dropdowns With Rails, jQuery, and AJAX
Populating collection_select field on selection of...
Generating dynamic content via ajax in Rails

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TyzhSysAdmin, 2015-08-11
@POS_troi

To start

url:      'update_subcategory'
#replace
url:      '/url'

/controller_name/function
and, accordingly, it can be viewed in the routes.
The coffee script compiler is not aware of what you have there with routes, take a look at the code that it compiled for you :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question