V
V
Vayladion Gognazdiak2017-06-07 20:27:13
JavaScript
Vayladion Gognazdiak, 2017-06-07 20:27:13

Why doesn't Rails4 render js?

Good day.
There is a select, on onchange of which an ajax request is made. In response, js should be rendered, which makes append the partial.
All requests are processed, both js and the partial are rendered in the logs, but the append does not occur.
Where to dig?
Rails 4.2.8, turbolinks included. Here is the JS
code in assets:

$( document ).on( "change", "#phone_vendor_select", function() {
  $.ajax({
    url: "unknown_devices/get_phone_models",
    dataType: 'script',
    data: {vendor: '11111'},
  });
});

View:
div.box-body
      table.table.table-bordered
        tbody
          tr
            th UserAgent
            th Вендор
            th Модель
            th Действия
          - @unknown_devices.each do |device|
            tr
              = form_tag unknown_devices_path, method: 'get', id: 'vendor',remote:true do
                td
                  = device.ua
                td
                  = select_tag "phone_vendor_select", options_from_collection_for_select(@phone_vendors, "id", "title"), include_blank: false
                td.mod_select id='mod_select'
                td
                  div.col-md-2
                    = submit_tag 'Сохранить', class: 'btn-sm btn-danger'

Controller:
def get_phone_models
    @phone_models = PhoneModel.all
    
    respond_to do |format|
      format.js
    end    
  end

JS.ERB
$(".mod_select").html('<%= render partial: "phone_model", collection: @phone_models %>');

And actually the phone_model partial (made in html.erb for simplicity)
<div>
<%= phone_model %>
</div>

Logs:
Started GET "/unknown_devices/get_phone_models?vendor=11111&_=1496856121592" for 127.0.0.1 at 2017-06-07 20:22:05 +0300
Processing by UnknownDevicesController#get_phone_models as JS
  Parameters: {"vendor"=>"11111", "_"=>"1496856121592"}

  PhoneModel Load (0.3ms)  SELECT "phone_models".* FROM "phone_models"
  Rendered cpan/unknown_devices/_phone_model.html.erb (1.1ms)
  Rendered cpan/unknown_devices/get_phone_models.js.erb within layouts/cpan (3.7ms)
  Rendered layouts/navigation/_header.html.erb (0.0ms)
  Rendered layouts/navigation/_sidebar.slim (0.9ms)
  Rendered layouts/navigation/_layout.slim (2.2ms)

As can be seen from the logs - everything works out, but unfortunately nothing appends.
Please tell me where to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2017-06-08
@etspring

dataType "script": Evaluates the response as JavaScript and returns it as plain text.

I'm not a super jquery expert, maybe you still need to use the success and error callbacks? In any case, you can insert alerts in js and check the js console for errors ...
In general, I would do this:
$.ajax({
    url: "unknown_devices/get_phone_models",
    dataType: 'text',
    data: {vendor: '11111'},
    success: function(data, textStatus, jqXHR) { $(".mod_select").html(data) },
    error: function(jqXHR, textStatus, errorThrown) { alert('Error') }
  });

And for get_phone_models give just html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question