Я пытаюсь переключить модал начальной загрузки 4, когда запись успешно обновлена.
если я делаю это вручную с консоли Chrome, он работает нормально, но из файла шаблона js.erb просто выдает ошибку:
Uncaught Error: Syntax error, unrecognized expression:
$('#mainModal').modal('toggle');
at Function.Sizzle.error (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:1542)
at Sizzle.tokenize (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:2194)
at Sizzle.select (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:2621)
at Function.Sizzle [as find] (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:846)
at jQuery.fn.init.find (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:2874)
at new jQuery.fn.init (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:2984)
at jQuery (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:140)
at HTMLFormElement.<anonymous> (modals.self-0fa40069f1b470beca9b1545857092a50efaaaab19a5aec03e92b45ce68e788b.js?body=1:25)
at HTMLDocument.dispatch (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:5184)
at HTMLDocument.elemData.handle (jquery3.self-5af507e253c37e9c9dcf65064fc3f93795e6e28012780579975a4d709f4074ad.js?body=1:4992)
однако - на самом деле он делает то, что должен делать, закрывает модальный режим и выполняет другие действия (обновление строки, показывая флэш-сообщение), которые не включены в файл js.erb для ясности ...
my view instrument.html.erb:
<table class="table" id="instruments-table">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Currency</th>
<th>P&L</th>
<th>Stuff</th>
</tr>
</thead>
<tbody>
<% @instruments.each do |instrument| %>
<tr>
<td><%= instrument.name %></td>
<td><%= instrument.description %></td>
<td><%= currency_to_iso(instrument.currency) %></td>
<td></td>
<td>
<%= link_to edit_instrument_path(instrument), class: 'my-2', 'data-toggle': 'tooltip', 'data-placement': 'top', title: 'Edit Instrument', data: { modal: true } do %>
<%= icon('far', 'edit') %>
<% end %>
<%= link_to new_account_instrument_path(instrument: instrument), class: 'my-2', 'data-toggle': 'tooltip', 'data-placement': 'top', title: 'Commissions', data: { modal: true } do %>
<%= icon('fas', 'coins') %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
my instrument_controller.rb:
class InstrumentsController < ApplicationController
respond_to :html, :json, :js
...
def update
@instrument = Instrument.find(params[:id])
if @instrument.update(instrument_params)
respond_with @instrument do |format|
format.js { flash.now[:notice] = 'All Good!' }
end
else
respond_modal_with @instrument
end
end
...
end
my update.js.erb:
$('#<%= dom_id(@instrument) %>').replaceWith('<%= j render 'users/dashboard/user_dashboard_instrument_row', instrument: @instrument %>');
$("#mainModal").modal("toggle");
$("#flash-wrapper").html("<%= escape_javascript(render 'layouts/flash_message') %>");
setTimeout(function(){
$('.alert-wrapper, .notice-wrapper').fadeOut("slow", function() {
$(this).remove();
})
}, 4500);
как указано выше, выполнение $('#mainModal').modal('toggle');
в консоли после открытия модального окна закрывает ее без проблем ..
сам модал сделан после великого гида на https://jtway.co/5-steps-to-add-remote-modals-to-your-rails-app-8c21213b4d0c
Итак, у меня есть modal_responder.rb
class ModalResponder < ActionController::Responder
cattr_accessor :modal_layout
self.modal_layout = 'modal'
def render(*args)
options = args.extract_options!
if request.xhr?
options.merge! layout: modal_layout
end
controller.render *args, options
end
def default_render(*args)
render(*args)
end
def redirect_to(options)
if request.xhr?
head :ok, location: controller.url_for(options)
else
controller.redirect_to(options)
end
end
end
и измененный application_controller.rb
class ApplicationController < ActionController::Base
...
def respond_modal_with(*args, &blk)
options = args.extract_options!
options[:responder] = ModalResponder
respond_with *args, options, &blk
end
...
end
и форма редактирования (я пропускаю саму форму, поскольку она не важна для этой проблемы)
<%= content_for :title, 'Edit Instrument' %>
<div class="modal-body">
<%= render 'form' %>
</div>
любая помощь высоко ценится!