Rails form not working after link_to - ruby-on-rails

When I click on a link and move to a new page, my form doesn't submit.
It works normally if I refresh though. It also works normally after disabling Turbolinks.
How can I resolve this and keep using Turbolinks?

jquery.turobolinks gem sorted out a similar issue i was having. Just make sure you include it directly after jquery.
It just makes all your js bindings etc still work with turbolinks

Related

Rails 5 Turbolinks appears to reload page

I am still new to Turbolinks and unsure whether it is working correctly. I have a new rails app that I've created with Turbolinks enabled by default. I am using the link_to rails helper to build some links. When I click on one of the links the page reloads in the browser. I was under the impression that this should not occur as only the body should be swapped using AJAX. Is there any additional attributes I need to add to my link or additional configuration I need to set up?
Make sure that you have the Turbolinks js loaded in the page. If you are using webpacker and removed the default application js that used the asset pipeline you will probably need to use Turbolinks from npm, import it and then call Turbolinks.start() in your entry file.

Rails needs page refresh to render correctly bootstrap markdown

I am developing an app using Ruby on Rails. In a specific view I have a textarea that uses Bootstrap Markdown.
The problem is that each time I visit that view, the textarea is rendered completely plain, without the Markdown functionality. Hitting F5 (refresh) renders the form correctly.
I tried to clear my browsers cache etc, but no luck. I always have to hit refresh to see the page correctly.
What may cause that?
Edit:
I am using this Markdown Plugin
I have also included the js files as stated in this question.
The turbolinks gem has been known to interfere with other javascript files and it's recommended that you remove it completely unless you really need it.
As Mohammad AbuShady said in his comment, you'll need to edit the part of the javascript files for your markdown that tells the page to start rendering. In your case this involves adding
$("#some-textarea").markdown({autofocus:false,savable:false})
inside page:load on the relevant pages.
In case you need turbolinks still, I found this very helpful: JQuery gets loaded only on page refresh in Rails 4 application
I had the same issue and adding jquery-turbolinks gem allowed the bootstrap-markdown editor to load perfectly for me.

Foundation 5's accordion not working with Rails 4

Foundation 5's accordion works great when I use it as per the documentation and open that page directly. However, if I click to another link on my site and then click to the page with the accordion, then the accordion doesn't expand when I click on the headings :(. I figured maybe it's the turbolinks JavaScript messing with Foundation's accordion. So we added this to the link to the page with the accordion:
<li data-no-turbolink><%= link_to 'My accordion page', accordion_page_path %></li>
Now it works better, but not perfect. If I go to my accordion page from another page, the accordion works. But then when I navigate away and click the back button on my browser, stops working :(.
This must be an extremely common problem as Foundation and Rails are very popular. Any way to get this to work?
Yes, turbolinks can cause many issues with RoR.
The reason turbolinks breaks your javascript is that document.ready events won't trigger properly. You need to do it on change instead.
Option 1. Revise all of the Javascript (Not easy or recommend)
var on_load = function() {
}
$(document).ready(on_load)
$(window).bind('page:change', on_load)
Option 2. Use the Jquery-Turbolinks gem.
The jquery turbolinks gem is designed to get around this problem. However, you need to read the docs, and make sure to install it properly. (It's a tricky beast.)
Option 3. Remove Turbolinks
If it's causing you to much trouble, you can just remove it. Delete it from the gemfile, remove turbolinks-tracked: true from your assets in the application layout, and wave turbolinks goodbye. I will admit, I have done this several of times. :\

No browser loading bar

I can't seem to find a similar issue
On my website I am running into an issue where the browser progress bar will not show until the page is completely rendered.
This is particularly bad on a very slow page (which I am working on). It makes it look like the link may have been broken instead of just taking a little while.
Looking at the network
There is a GET method on the current page which returns a 304, this runs for about 3-5 seconds.
Once that finishes the new website will load with a near instant progress bar.
I am not sure what code I can share since this is happening everywhere on my site, it is just more noticeable on certain pages.
To see it at its worse go to http://www.swtorconquest.com/conquestweeks and click either "The Trade Emporium" or "Clash in Hyperspace".
I am having this issue both when testing locally and when the site is deployed.
I am using ruby 2.1.2 and rails 4.1.5
Do one thing, in application.html.erb, in body tag add this, it will solve your problem
<body data-no-turbolink>
The problem will almost certainly be Turbolinks, although this is just a guess. Lacking any other answers, I'll hopefully be able to give you some ideas:
When you load a page with Turbolinks, it will actually load the <body> tag of the page through Ajax, leading the <head> intact.
This causes a lot of problems if not handled correctly, one of which (we've found) being that your browser can no longer determine how quickly the page is loading. This is likely what your problem is.
Although I don't have an outright fix, I do have a test.
You can try removing any references to Turbolinks throughout the various pages in this part of your application. This can be done by using the following:
#app/views/layouts/application.html.erb
<%= javascript_include_tag "application" %> => remove the turbolinks reference
<%= stylesheet_link_tag "application" %> => remove the turbolinks reference
Also, you need to remove Turbolinks from your Gemfile & your application.js:
#app/assets/javascripts/application.js
//= require turbolinks => remove this line
This will give you the ability to gauge whether it's turbolinks which is preventing the status bar from loading correctly. If it is, then you'll have to work around this (I don't have any remedies off hand)
It must be noted that this test will not speed up the load time - it will merely show whether the status bar issue is caused by Turbolinks or not

Can't get pjax to work on Rails

I'm trying to get pjax to work on a Rails app but none of the links are being annotated with pjax. I think pjax isn't really being loaded. I'm using pjax_rails and am basically following the railscast instructions but using //= require jquery.pjax instead of just pjax. I'm also using it with bootstrap which may cause an issue but I'm not sure. My other thought is that is that the pjax javascript isn't being loaded and I need to run something like $('a').pjax('[data-pjax-container]')
To be clear the main problem is that pjax isn't being loaded client side and thus when I make requests the X-PJAX header is not being set.
Finally got it working. First problem was that the unbeknownst to me the assests pipeline was turned off so the pjax javascript file wasn't being included. Instead I went with the pjax rack gem and dumped it into the public directory. Then I created a new javascript file with $('a').pjax('[data-pjax-container]') and it works! Although I'm not sure why I need to manually call that javascript when all the documentation seems to point to the fact that it should be already there.

Resources