ujs

Turbolinks causes a link with href=“#” to trigger page refresh

依然范特西╮ 提交于 2019-12-04 09:58:35
I have a very simple link on my page. <a href="#">My link</a> . It causes a page refresh. when I remove "turbolinks", it no longer causes a refresh. I've used links with hash fragments all the time in the past. Unless I've missed something very fundamental for a long time, I don't understand how this can cause a refresh. It does not have any JS event handlers attached to it. Any ideas? It may not matter, but I'm using jQuery, Twitter-Bootstrap, and Ruby on Rails. Clues so far: When I remove Turbolinks, the link behaves properly (does not cause page refresh). Links with "#" as the URL cause a

With Rails UJS, how to submit a remote form from a function

拈花ヽ惹草 提交于 2019-12-03 16:21:35
问题 I'm using Rails UJS. I have a form setup to do a remote submit like so: <form accept-charset="UTF-8" action="/subscriptions" class="new_subscription form-horizontal" id="new_subscription" data-remote="true" data-type="json" method="POST"> I'm trying to find a way to submit this form from a JavaScript function. I've tried: var form$ = $("#new_subscription"); form$.get(0).submit(); but the problem with this is that it submits the form w/o the remote, it posts to the server and refreshes the

Rails 3 UJS driver events

非 Y 不嫁゛ 提交于 2019-12-03 10:06:49
According to Simone Carletti blog post , Rails 3 ajax helpers have changed a lot. We are supposed to write more javascript with rails 3 than we used to with rails 2. I tried to figure out how to show up an ajax loading gif -while an ajax query is running- in the "rails 3 way". I came up with this kind of code, which uses javascript events sent by the Rails 3 UJS driver. This example uses prototype: <div id="wait" style="display:none"> <img src="/images/ajax-loader.gif"> Please wait... </div> <div> <%= link_to 'Get', 'finished', :id => "mylink", :remote => true %> </div> <%= javascript_tag do %

How do I stop a remote form from submitting?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 08:30:07
I have a form that can be used both remotely and normally. = form_for @comment, html: { class: 'comment-form' }, remote: request.xhr? do |f| = f.text_area :body = f.submit I want the form to submit only if the body textarea has content: $(document).on 'submit', '.comment-form', (e) -> text = $(this).find('#comment_body').val() false if text.length < 1 That code works when the form is not Ajax. But when it's remote, it fails and the form still submits. Why? I also tried: if text.length < 1 e.preventDefault() false I'm using Rails 4 with Turbolinks. Update: I tried binding the ajax:beforeSend

How to render new.js.coffee.erb in app/views?

柔情痞子 提交于 2019-12-03 06:34:53
问题 Using Rails 3.1 jquery_ujs, I have a link with :remote => true, and the controller new action responds with js and render new.js.erb which contains: $('#post-form').html('<%= escape_javascript(render(:partial => "form")) %>'); It renders the _form.html.erb partial. This works. Now I want to use Coffeescript, but renaming new.js.erb to new.js.coffee.erb doesn't work. Does the asset pipeline only work in app/assets? What am I doing wrong? Thanks. 回答1: I had this same issue using Rails 3.1.0.

With Rails UJS, how to submit a remote form from a function

我怕爱的太早我们不能终老 提交于 2019-12-03 05:38:42
I'm using Rails UJS. I have a form setup to do a remote submit like so: <form accept-charset="UTF-8" action="/subscriptions" class="new_subscription form-horizontal" id="new_subscription" data-remote="true" data-type="json" method="POST"> I'm trying to find a way to submit this form from a JavaScript function. I've tried: var form$ = $("#new_subscription"); form$.get(0).submit(); but the problem with this is that it submits the form w/o the remote, it posts to the server and refreshes the page. Any idea why that is? Is there a different way to submit a remote form? Thanks padi Perhaps for

Rails controller - execute action only if the a Rails UJS method inside succeed (mutually dependent methods)

懵懂的女人 提交于 2019-12-02 21:54:35
问题 Following another question (Rails controller - execute action only if the two methods inside succeed (mutually dependent methods)), I would like to ensure that inside one of my controller's action, if the user does not see the message displayed by a Rails UJS method, then the first methods of the controller action are not implemented either. CONTEXT I have a Controller with a method called 'actions'. When the app goes inside the method 'example_action', it implements a first method (1) update

How to render new.js.coffee.erb in app/views?

穿精又带淫゛_ 提交于 2019-12-02 20:13:05
Using Rails 3.1 jquery_ujs, I have a link with :remote => true, and the controller new action responds with js and render new.js.erb which contains: $('#post-form').html('<%= escape_javascript(render(:partial => "form")) %>'); It renders the _form.html.erb partial. This works. Now I want to use Coffeescript, but renaming new.js.erb to new.js.coffee.erb doesn't work. Does the asset pipeline only work in app/assets? What am I doing wrong? Thanks. I had this same issue using Rails 3.1.0. Try renaming your file to just new.js.coffee . It should still render erb despite not having the extension on

Rails controller - execute action only if the a Rails UJS method inside succeed (mutually dependent methods)

我只是一个虾纸丫 提交于 2019-12-02 09:03:53
Following another question ( Rails controller - execute action only if the two methods inside succeed (mutually dependent methods) ), I would like to ensure that inside one of my controller's action, if the user does not see the message displayed by a Rails UJS method, then the first methods of the controller action are not implemented either. CONTEXT I have a Controller with a method called 'actions'. When the app goes inside the method 'example_action', it implements a first method (1) update_user_table and then (2) another update_userdeal_table. (both will read and write database) and then

Why I need add “data: {type: ”script“}” on remote link with rails / ajax

空扰寡人 提交于 2019-12-01 18:04:12
问题 in one project of mine, the code: = link_to "add", new_me_category_path, class: "btn btn-success", remote: true can load remote form correctly. But some one can not work, browser did not execute the responese js code. I need to add "data: {type: "script"}" like this : = link_to "add", new_me_category_path, class: "btn btn-success", remote: true, data: {type: "script"} I want to know the reason. 回答1: Im not JS expert, and I dont know Ruby, but i think: When datatype is set to script -