ujs

How to use Ruby code inside js.erb?

萝らか妹 提交于 2021-01-03 08:42:45
问题 I'm able to render partial inside a modal using escape_javascript in js.erb file code: $("body").append("<%= escape_javascript(render partial: 'example_partial') %>"); $('#my_modal').modal('show'); However, I can't seem to get results for: console.log(<%= @error %>) 回答1: ERB will output a plain string. JS needs inverted commas around a string for it to be recognized. You have missed them on your console.log() statement. Change it to: console.log('<%= @error %>'); You may also find the raw

How to use Ruby code inside js.erb?

笑着哭i 提交于 2021-01-03 08:42:19
问题 I'm able to render partial inside a modal using escape_javascript in js.erb file code: $("body").append("<%= escape_javascript(render partial: 'example_partial') %>"); $('#my_modal').modal('show'); However, I can't seem to get results for: console.log(<%= @error %>) 回答1: ERB will output a plain string. JS needs inverted commas around a string for it to be recognized. You have missed them on your console.log() statement. Change it to: console.log('<%= @error %>'); You may also find the raw

Rails form_for with file_field and remote => true and format => :js

做~自己de王妃 提交于 2020-01-21 12:22:09
问题 I am submitting a form_for with a file_field in it. When the form is submitted with a post, the controller responds with a .js file which will be executed. The file_field is optional. When I upload a file, the js file is opened in a new document (as a simple text file) and the js is not executed. When I don't upload any file, the response div is updated. I want the update success to be notified in the response div or a failure message independent of the file upload. May be this is done for

Rails unobtrusive javascript (UJS) ajax:before append data params

你。 提交于 2020-01-02 10:20:17
问题 I'm trying to append some parameters to a form before sending via ajax (via Rails UJS). $(document).on("ajax:before",".form-example", function(event) { event.data = $(":hidden").serialize(); }); The hidden inputs aren't within the form, so I need to collect them, serialize the, and add them to the request (which is a PUT request). However, in the above example, no parameters are being passed to the server, am I doing something wrong? How can I pass parameter called "items" with the serialized

Rails 3 - Periodic Refresh of DIV

只谈情不闲聊 提交于 2020-01-01 19:55:14
问题 I am trying to refresh the contents of a div using AJAX/Unobtrusive JavaScript/JQuery. This is easy to do if I make a "refresh" link and give it a data-remote attribute. However, I would like to refresh every 2 seconds using the setInterval function rather than using a link. This code should demonstrate what I'm trying to do. It does not work when put in application.js because the ERB is not evaluated, but hopefully it will explain my goal. setInterval(function(){ $("#scuttlebutt").html("<%=

Rails UJS “on” for handling ajax event

半世苍凉 提交于 2019-12-24 03:16:52
问题 I'm using the jQuery method "on" in a Rails app to attach an event on a form which doesn't always exist. It doesn't seem to be attaching an event handler when #myForm is appended to the document. Here is UJS's suggested usage of on() : $("#myForm").on("ajax:complete", function(xhr, status) { ... } Here is how I usually implement jQuery's on() , but this does not seem to work: $("body").on("ajax:complete", "#myForm", function(xhr, status) {...} Rails UJS wiki: https://github.com/rails/jquery

how to set timeout in rails UJS?

末鹿安然 提交于 2019-12-23 12:11:16
问题 The new and cool syntax allows me to write: link_to some_path, :remote => true to generate an AJAX request. But if I need longer timeout(e.g. 100000ms), where can I set it? I read link_to but found nothing. 回答1: You can use $.rails.ajax property from jquery-ujs, to inject the timeout if it has not been passed explicitly: $(function() { // ... $.rails.ajax = function(options) { if (!options.timeout) { options.timeout = 100000; } return $.ajax(options); }; // ... }); 来源: https://stackoverflow

How to modify the ajax Post data in a beforeSend event?

非 Y 不嫁゛ 提交于 2019-12-22 09:39:44
问题 Hello I have a form that submits remotely with the jQuery UJS for rails. I binded to the beforeSend event to allow me to modify the data submitting to the serve. It's not working. Here is what I have in the beforeSend: settings.data = JSON.stringify({ 'list_item[title]' : 'hi?? there' }) This doesn't work. In the server logs I see this: Started POST "/lists/9/list_items" for 127.0.0.1 at 2011-10-24 14:04:36 -0700 Processing by ListItemsController#create as JSON Parameters: {"{\"list_item"=>{