Answer the question
In order to leave comments, you need to log in
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'},
});
});
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'
def get_phone_models
@phone_models = PhoneModel.all
respond_to do |format|
format.js
end
end
$(".mod_select").html('<%= render partial: "phone_model", collection: @phone_models %>');
<div>
<%= phone_model %>
</div>
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)
Answer the question
In order to leave comments, you need to log in
dataType "script": Evaluates the response as JavaScript and returns it as plain text.
$.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') }
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question