Turbolinks causes JavaScript file to run several times - ruby-on-rails

I'm using Rails 5.1.4, I'm new on the platform and I have problems with turboblinks, I have a file containing jquery code that has to run once the page loads, but Whenever the page loads the file runs several times but when I disable the turboblinks there it works as I want it. So I would like to know if there is one less to fix it without turning off the turboblinks. Thank you in advance..
So there is my layout file.// app/views/layouts/application.html.erb
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Rails web app</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-
track' => true %>
<%= csrf_meta_tags %>
</head>
<body class="<%= controller_name %> <%= action_name %>">
<%= render 'layouts/header' %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<div class="container">
<%= yield %>
</div>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true
%>
</body>
</html>
About my js file as you will see i try to load automatically a bootstrap modal with an animation when the page loaded, actually the problem happens when i clik to any link on this page which takes me to another page and i try to come back to the initial page where the js file is loaded, then the problem happens. i see the page refresh many times and the js file as well.
So there is my js file with jquery code. //app/assets/javascripts/file.js
$(document).ready(function(){
$('#myModal').on('hide.bs.modal', function (e) {
$('.modal .modal-dialog').attr('class', 'modal-dialog animated rollOut');
});
$('#myModal').on('show.bs.modal', function (e) {
$('.modal .modal-dialog').attr('class', 'modal-dialog fadeIn
animated');
});
$('#myModal').modal('toggle');
});

Turbolinks replaces the body so put this line in your head tag :
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
Like this :
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Rails web app</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= csrf_meta_tags %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
</head>
<body class="<%= controller_name %> <%= action_name %>">
<%= render 'layouts/header' %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<div class="container">
<%= yield %>
</div>
</body>
</html>
or add data-turbolinks-eval=false to the script tag that includes turbolinks.
Also you need use the turbolinks:load event :
$(document).on('turbolinks:load', function() {
$('#myModal').on('hide.bs.modal', function (e) {
$('.modal .modal-dialog').attr('class', 'modal-dialog animated rollOut');
});
$('#myModal').on('show.bs.modal', function (e) {
$('.modal .modal-dialog').attr('class', 'modal-dialog fadeIn
animated');
});
$('#myModal').modal('toggle');
});

Related

Rails app with Boostrap not mobile response

After adding the viewport meta tag, my app still is not mobile responsive. I am using bootstrap as well. Is there any fix?
application.html.erb
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>gustoHop</title>
<%= csrf_meta_tags %>
<%= action_cable_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%#= stylesheet_pack_tag 'application', media: 'all' %> <!-- Uncomment if you import CSS in app/javascript/packs/application.js -->
<!-- <style>
#import url('https://fonts.googleapis.com/css2?family=Inconsolata:wght#400;600;700&display=swap');
</style> -->
<%= favicon_link_tag asset_path('gustoHop-favicon.png') %>
</head>
<body>
<%= render "shared/navbar" %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
<%= render "shared/footer" %>
<%= javascript_pack_tag 'application' %>
</body>
</html>

RAILS Prism gem only highlights codesample from direct URL or when refreshing page

I am using the prism gem and it highlights great but only from a direct URL or after refreshing the page. It does not highlight if navigating from the home page.
Any suggestions? Thanks!
<!DOCTYPE html>
<html>
<head>
<link href="themes/prism.css" rel="stylesheet" />
<script src="prism.js"></script>
<title><%= yield(:page_title) %></title>
<%= csrf_meta_tags %>
<link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= render "layouts/navbar" %>
<div class="container <%= controller_name %> <%= action_name %>">
<%= yield %>
</div>
</body>
</html>

How write this code in HAML

How write this ERB code in HAML:
<head>
<meta charset="utf-8">
<title><%= title %></title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<%== meta_data_tags %>
<%= favicon_link_tag image_path('favicon.ico') %>
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="apple-touch-icon-114x114.png">
<%= stylesheet_link_tag 'store/all', :media => 'screen' %>
<%= csrf_meta_tags %>
<%= javascript_include_tag 'store/all' %>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6/html5shiv.min.js"></script>
<![endif]-->
<%= render "spree/shared/routes" %>
<%= yield :head %>
</head>
particularly interested in this
<%== meta_data_tags %>
code snippet.
I found answer for my question:
= raw(meta_data_tags)
<%== %> is the same as <%= raw() %>. It's just a fast helper to achieve same result.
Thank you all for help. =)
In Erubis (whch Rails uses) the <%== ... %> syntax escapes the result of the expression. Haml has a similar feature using &=.
So a Haml version of <%== meta_data_tags %> might look something like:
&= meta_data_tags

Capybara ambiguous match - should have_content

I'm using Capybara and Rspec in a Rails app, and I keep failing some of my tests with this message :
Failure/Error: it { should have_content('error') }
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching xpath "/html"
And that's kind of logical, because in my test, my app should render two messages with the 'error' content.
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
<%= t('the_form_contains') %>
<%= pluralize(object.errors.count, t('error')) %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
And here my application layout :
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<div class="row-fluid">
<div class="offset2 span8 offset2">
<% flash.each do |key,value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
</div>
</div>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
Do you know a way (like an option), to make it pass ?
EDIT
I used save_and_open_page and I didn't found any additional html tag, neither error messages outside the page. But when I remove <!DOCTYPE html> then it works. Must I really have <!DOCTYPE html> and an opening <html> tag ? Can I delete <!DOCTYPE html> without consequences ?
<!DOCTYPE html>
<html>
<head>
<title>Le Troquet</title>
<link data-turbolinks-track="true" href="/assets/application.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application.js"></script>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
.
.
.
.
.
<!-- There is no html tag here -->
.
.
.
.
</body>
</html>
It looks like the problem is that you either:
have two <html> tags in your page, or possibly
the "error" text appears outside the <html></html> tags.
See this discussion: Ambiguous match, found 2 elements matching xpath "/html".
Check for elements outside of the closing </html> tag. In my case (HAML), I had a javascript tag that was improperly indented
%html
...
:javascript
FIX
%html
...
:javascript
...
Indenting the :javascript so it was contained within the html tag solved the issue.
OK, I understand the problem. When I use should have_content, Capybara search in the whole layout the content. But it finds two html tags, that make it fails, because it doesn't know which is the right one. And why is there two html tags ? Because Capybara doesn't ignore the <!DOCTYPE html>.
I solved my problem, but it's more of a bypass. Instead of searching a content, I made Capybara search for the 'error' div, that contains the 'error' messages. Thereby, it doesn't have to search the whole application layout, and it doesn't get stucked with the two html tags.
So I just had to replace it { should have_content('error') } by it { should have_selector('div', text: 'error') }, and it worked.

stylesheet_link_tag not producing links

I'm working through the Ruby on Rails Tutorial at http://railstutorial.org/, chapter 4, listing 4.4. I've inserted the two lines that have calls to stylesheet_link_tag, but the links aren't being inserted into the final page. Here's my application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
<% stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
<% stylesheet_link_tag 'blueprint/print', :media => 'print' %>
</head>
<body>
<%= yield %>
</body>
</html>
Here's the output of http://localhost:3000/pages/home
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | Home</title>
<meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="D8xx1zqWM5qqwqdabyjy5eHLPvLY/Sxe5vEFJ816fMY="/>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application
</p>
</body>
</html>
I've copied and pasted and typed on my own, and I'm getting the same results. Suggestions?
Thanks,
Chuck
This is producing output so it needs to be in an erb block that outputs into the template:
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
the type of block you are using:
<% stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
does not have its result printed into the template.

Resources