Created
May 13, 2015 06:22
-
-
Save reiro/5bc9310c544de43af122 to your computer and use it in GitHub Desktop.
Динамические селекты
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Контроллер | |
| def update_service_types | |
| @service_types = ServiceType.where("sto_category_id = ?", params[:sto_category_id]) | |
| respond_to do |format| | |
| format.js | |
| end | |
| end | |
| #assets/js/autoservice.js.coffee | |
| $ -> | |
| $(document).on 'change', '#sto_categories_select', (evt) -> | |
| $.ajax 'autoservices/update_service_types', | |
| type: 'GET' | |
| dataType: 'script' | |
| data: { | |
| sto_category_id: $("#sto_categories_select option:selected").val() | |
| } | |
| error: (jqXHR, textStatus, errorThrown) -> | |
| console.log("AJAX Error: #{textStatus}") | |
| success: (data, textStatus, jqXHR) -> | |
| console.log("Dynamic country select OK!") | |
| #update_service_types.js.coffee | |
| $("#service_types_select").empty() | |
| .append("<%= escape_javascript(render(:partial => @service_types)) %>") | |
| # _service_type.html.erb | |
| <option value="<%= service_type.id %>"><%= service_type.name %></option> | |
| #view | |
| <div class="control-group"> | |
| <label>Выберите услугу</label> | |
| <div class="controls"> | |
| <%= f.select :sto_category_id, options_for_select(StoCategory.all.collect { |category| | |
| [category.name, category.id] }, 0), {:include_blank => "Выберите категорию"}, { id: 'sto_categories_select', class: "form-control" } %> | |
| </div> | |
| </div> | |
| <div class="control-group"> | |
| <label>Выберите вид услуги</label> | |
| <div class="controls"> | |
| <%= f.select :service_type_id, options_for_select(ServiceType.all.collect { |service_type| | |
| [service_type.name, service_type.id] }, 0), {:include_blank => ""}, { id: 'service_types_select', class: "form-control" } %> | |
| </div> | |
| </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment