Mixpanel with Turbo Rails - ruby-on-rails

I'm getting the following error while trying to use the Turbo Rails link:
Mixpanel error: The Mixpanel library has already been downloaded at least once. Ensure that the Mixpanel code snippet only appears once on the page (and is not double-loaded by a tag manager) in order to avoid errors.
My layout code is:
<HTML>
<head>
<%= mixpanel_code %>
</head>
<body>
...
<%= link_to "Settings", my_settings_path %>
...
</body>
</HTML>
This issue doesn’t exist if Turbo Rails is disabled.
What’s the correct way to use mixpanel with Turbo?
Thanks

Related

include a javascript file only on signup page - Rails & Turbolinks

I need to include a JS file only on the signup page of my rails application. I am using Turbolinks.
I have the below code in application.html.erb just before the <head> section
<%= yield :page_specific%>
and below is the code in my registrations/new.html.erb
<%content_for :page_specific do%>
<%= javascript_include_tag 'page_specific/users/registrations/index', 'data-turbolinks-track' => true%>
<%end%>
This works as expected and loads the script on the registrations page, but if I visit the login page from registrations page, the script remains included in the head section as i have turbolinks enabled.
How can include a page specific javascript only on signup page with turbolinks enabled.
Not sure if its the most elegant way but I managed to fix the issue by disabling turoblinks on login and sign up link by using :'data-turbolinks' => "false"
Hope it helps. Thanks.
Just include javascript_include_tag into view of target page

run coffeescript if user is present

I'm trying to find a way to only run a specific piece of coffeescript code if the user is signed in. Is there a way to achieve this without having to include that specific coffeescript file. For example check if a session cookie with the value of user_id is found, then run this piece of code.
The goal is to remove some inline javascript and move them over to the assets.
Rails by default uses ActionDispatch::Session::CookieStore so theoretically you could read the cookie and get the user id. However to avoid spoofing and session based attacks the cookie is encrypted. And you can't decrypt it without exposing the Rails secret_key_base to the client - which is a really bad idea or forgoing the protection of encrypted cookies.
Instead might just want to let your view / layout tell you if there is a signed in user:
<!DOCTYPE html>
<html>
<head>
<title>Chess</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<%= content_tag(:body, data: { "signed-in" => user_signed_in?.to_s }) do %>
<%= yield %>
<% end %>
</html>
This will let you read $('body').data('signed_in') and avoids exposing / duplicating any of the actual authentication logic on the client. This also works well with Turbolinks since Turbolinks replaces the body tag.
You would use it like so:
// #return Boolean
function is_user_signed_in(){
$('body').data('signed-in') === 'true';
}
You can move your javascript out to assets.
Expanding on max's answer, it can then look at the value of $('body').data('signed_in')
for example:
if($('body').data('signed_in') == true){
//execute
}
So your javascript still needs to be included in the client's browser but using some logic like that above allows you to only run if there is a user signed in.

Rails 4 x premailer x SASS: import stylesheet in mailer view

In my Rails 4 app, I use mailers to send out emails to users.
These mailers are working fine.
Now, I would like to style them, so I installed the premailer-rails gem (and the nokogiri gem).
I restarted my server.
Then, I created a specific stylesheet for mailers: public/assets/mailers.scss
I my mailer view, I added:
<head>
<% stylesheet_link_tag mailers "public/assets/mailers.scss" %>
</head>
However, when a user opens the email in his inbox, for instance Gmail, the style is not applied.
Is there a particular way to include stylesheets in mailer views in Rails with premailer when using SASS?
What is mailers in you code snippet? If it is some helper that returns the name of the css file, then it's ok. See here in documentation what is expected by stylesheet_link_tag method.
Anyway, whatever it is, you are missing = sign, and you need it in order to render anything into you view:
<head>
<%= stylesheet_link_tag mailers "public/assets/mailers.scss" %>
</head>
No,there is no certain way to conclude stylesheet.

How do I link to an external stylesheet in Ruby on Rails?

I'm trying to link to the YUI reset stylesheet in my RoR layout using the following statement...
<%= stylesheet_path("http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css") %>
but this path is being echoed on my page instead of being applied. I got this syntax from the the rails docs. What am I doing wrong?
Thanks!
Try this
<%= stylesheet_link_tag 'application','http://yui.yahooapis.com/2.8.1/build/reset/reset-min.css' %>
Try stylesheet_link_tag as described in the Rails API Docs

Rails: using "content_for" after the corresponding "yield" inside layout

I think this has been asked before but even though I searched Google I haven't come up with a solution.
So this is what I'm trying to do in Rails 2.3.5:
layouts/application.html.erb:
<html>
<head>
... some other stuff
<%= yield :head %>
</head>
<body>
<% content_for :head, "something that belongs in the head" %>
</body>
</html>
Notice the yield before the content_for.
I know that Rails - by default - doesn't allow the content of :head to be defined after yield has been used - makes sense.
I even tried hooking into the template render process but no success so far.
So my goal is to be able to define content_for inside partials/templates and have the "yield" somehow delayed and executed just before the response is send to the browser.
Has somebody come up with a solution?
Greetings and thanks,
Frank
Update
I'll go with weppos's idea and try myself on rack middleware. thanks
The rendering process first loads and executes the action template, then decorates the template with the selected layout.
The layout is rendered from top to botton, thus you can't add more content to :head after :head is already rendered.
You need to change your strategy. Either place the fragment in a partial and attach it to your action views or use a post-processing strategy such as a Rack module/after_filter to alter the html code directly.
I probably would try to find a better solution based on what I actually need. If you are encountering this issue, chances are the error is somewhere else, perhaps in the app architecture.
There shouldn't be an equals sign in your content_for statement. It should be:
<% content_for :head, "Something that belongs in the head" %>
If you define the content within your templates and partials then it should work. This technique was covered in Railscast episode 8.

Resources