October 30, 2026

How to Add Live Chat to Ruby on Rails: Layout, Turbo and Environments

Add live chat to Ruby on Rails: a layout partial rendering the Talkmio chat snippet before the closing body tag

To add live chat to Ruby on Rails, you put the Talkmio install snippet in a partial, render that partial from your application layout just before the closing </body> tag and switch it on only in production. That is the whole integration. The parts worth getting right are where the partial lives, how to keep the site key out of your views, how the widget behaves with Turbo page navigation and what to change if you run a strict content security policy. This guide walks through each one so you can finish in about fifteen minutes.

Talkmio is an AI live chat. Its assistant, Mio, reads your website and the documents you upload, answers visitors in their own language and hands the chat to your team when it cannot answer from your content. The Rails side is only the delivery of one script.

What You Need Before You Start

You need a Rails application you can deploy, a free account at app.talkmio.com and a site key. In Talkmio, open Websites, choose Add and enter your domain. Talkmio then shows the site key and the install snippet. This guide uses a placeholder instead of printing the snippet, so you always paste the current version from your own account.

The steps are the same for Rails 7 and later, with or without importmaps, Webpacker replacements or a JavaScript bundler, because the snippet is a plain script tag and does not go through your asset pipeline. You do not install a gem, add an npm package or change your bundle.

Add Live Chat to Ruby on Rails Through a Layout Partial

Create a partial for the snippet, for example app/views/shared/_talkmio.html.erb, and paste the snippet from Websites into it. Then render it from the layout that wraps your public pages, usually app/views/layouts/application.html.erb, just above the closing body tag:

<body>
  <%= yield %>
  <%= render "shared/talkmio" if Rails.env.production? %>
</body>

Deploy, open any page and the chat bubble should appear in the corner. If your application has more than one layout, for example one for a logged-in dashboard and one for the public site, render the partial only in the layout that visitors see. A sales chat inside an admin screen is rarely wanted, although a support chat inside a customer app can be valuable if you want customers to reach your team from there. The Rails guide to layouts and rendering explains how layouts are chosen per controller.

Keep It Off in Development and Test

A chat widget on your laptop is noise: you will open conversations from your own test visits and teach yourself to ignore the bubble. The condition above, Rails.env.production?, is the simplest switch. Many teams prefer a setting that also works for staging, which should either stay quiet or use a separate Talkmio website entry with its own key.

Approach How Best for
Environment check Render the partial only if Rails.env.production? Small apps with one live environment
Config flag Add config.x.talkmio_enabled in each environment file Staging that should stay quiet while production is live
Separate key per environment Create a second website in Talkmio for staging and read its key from credentials Teams that want to test the chat before launch

The custom configuration option is a one-line change: set config.x.talkmio_enabled = true in config/environments/production.rb and check Rails.configuration.x.talkmio_enabled in the layout. A missing value is simply nil, so every other environment stays off without extra code.

Store the Site Key in Credentials, Not in HTML

Hard-coding the site key in a partial works, but it ties the view to one environment and puts an identifier in your repository history. Rails gives you better places. Put the key in encrypted credentials with bin/rails credentials:edit under a name such as talkmio: site_key, and read it in the partial with Rails.application.credentials.dig(:talkmio, :site_key). If you deploy with environment variables instead, use ENV.fetch("TALKMIO_SITE_KEY", nil) and skip rendering when it is empty.

A site key is meant to appear in the page source, because the browser needs it, so this is about tidiness and per-environment values, not secrecy. Never put an account password or an API token in a view.

Turbo, Page Navigation and the Widget

Modern Rails applications use Turbo, which replaces the page body on navigation instead of reloading the whole document. That changes how scripts behave, so test it rather than assume. The Turbo Drive handbook describes how it merges the head and replaces the body, and how script elements in the new body are evaluated again.

Run a short check after deploying. Open the site, click through five internal links without a full reload, and confirm that exactly one chat bubble is visible, that it opens and that a conversation you start on one page continues on the next. If the bubble disappears, flickers or appears twice, the cause is almost always the interaction between the snippet and Turbo, and the fix is a matter of where the script is evaluated. Send us the page address at [email protected] and we will help, rather than guess at a workaround that could break the widget.

If you disable Turbo Drive for a few pages, for example with data-turbo="false" on the links, those navigations load the full document and behave like a classic Rails site.

Content Security Policy in Rails

Rails ships a content security policy initializer at config/initializers/content_security_policy.rb. When the policy is enabled, a strict script_src blocks scripts from hosts it does not know, so the chat bubble does not appear and the browser console shows a violation. The fix is to allow the host the snippet loads its script from, which you can read from the snippet itself.

Start in report-only mode: set config.content_security_policy_report_only = true, load a page, read the console reports and allow only what is reported. If you use nonces, set config.content_security_policy_nonce_generator and give the snippet the same nonce. In a partial, javascript_tag nonce: true adds it for inline scripts. The Rails security guide covers the options in detail. Our article on chat widget cookies and consent explains the privacy side of loading third-party scripts.

Caching, Turbolinks Leftovers and Other Gotchas

Full-page caches and CDNs store the snippet together with the page, which is fine because it is the same for every visitor. The only problem arises if you wrap the snippet in a condition that depends on the user, such as showing it to anonymous visitors only, and then cache the result for everyone. Base the condition on the environment, not on the visitor, or vary the cache correctly.

Older applications sometimes still use Turbolinks. The same test applies: click through several pages and look for duplicate bubbles. Engines and mountable apps usually have their own layouts, so check that the partial is rendered where you expect. Pages rendered from ActionMailer or PDF views do not use the layout and do not need the snippet.

Staging, Review Apps and Deploy Checks

Rails teams often run staging servers and temporary review apps for each pull request. Decide up front what the chat does there. The safest default is nothing: render the partial only in production, so reviewers never talk to your real inbox by accident. If you want to test the full flow, create a second website entry in Talkmio for staging, store its key in the staging credentials and keep the knowledge base small, because the goal is to check delivery, not to train Mio twice.

Add one line to your deploy checklist: after each release, open the live site in a private window and confirm that the bubble appears and opens. It takes ten seconds and catches the most common failure, a layout refactor that quietly drops the render call. If you use feature tests, a simple assertion that the production layout includes the partial is enough, and it protects the integration against future template changes by people who did not set it up.

Teach Mio Before You Announce It

With the widget visible, spend your time on the knowledge. In Talkmio, open Mio AI and then Knowledge base, add your website and Mio reads every public page on the same domain while respecting robots.txt. Add the documents that do not live on a page, such as a PDF price list or terms of service. Then write business instructions that state what Mio must not promise, for example never to quote a delivery date, and use Try Mio to test answers and see which knowledge pieces were used. Our guide to testing an AI chatbot before launch has a list of questions to try.

If your team uses Rails for a shop or a software product, look at the sibling guides for Laravel and Next.js, because the structure of the work is the same: one snippet, one layout, one switch for environments.

One last Rails habit is worth mentioning: keep the partial small and boring. Do not add conditions that depend on the current user, the controller or the request path unless you have a clear reason, because every condition is another way for the bubble to vanish without anyone noticing. If you later want the chat only on selected pages, such as pricing and product pages, handle it with a content block in the layout and test each of those pages, not with logic scattered through your views.

Troubleshooting a Missing Bubble

When the bubble does not appear, work through four checks in order. Is the partial rendered by the layout that the page actually uses? Is the condition true in the environment you are testing? Does the page source in the browser show the script tag? Does the console report a blocked request from a content security policy or an ad blocker? Fixing the first one that fails solves most cases. If all four look right, test in a private window with extensions disabled, because privacy extensions block many chat widgets.

Plans and limits are on the Talkmio pricing page, and the Free plan, with one operator, one website and 50 AI answers a month, is enough to prove that the integration works.

Frequently Asked Questions

Do I need a gem to add live chat to Rails?

No. The Talkmio install snippet is a plain script tag. You paste it into a partial and render the partial from your layout, so there is nothing to add to your Gemfile, your importmap or your JavaScript bundle.

Where should the snippet go in a Rails layout?

Just before the closing body tag of the layout that wraps your public pages, which is usually application.html.erb. Keep it out of layouts used for admin or dashboard screens, unless you deliberately want visitors to reach your team from inside the application.

How do I hide the widget in development?

Render the partial only when Rails.env.production? is true, or use a custom setting such as config.x.talkmio_enabled that is switched on in the production environment file. A separate Talkmio website with its own key works well when you want to test on staging.

Does the chat work with Turbo?

Test it after deploying. Click through several internal links and confirm that one bubble stays visible and a started conversation continues. If you see a duplicate or missing bubble, write to [email protected] with the page address so we can help.

What do I change if I use a content security policy?

Allow the host that the snippet loads its script from in script_src, or give the snippet your nonce. Start in report-only mode, read the browser reports, allow only what is actually reported and then switch the policy to enforcing mode.

Is it free to try?

Yes. The Free plan includes one operator, one website, 50 AI answers a month and 50 tickets a month, with no card needed. Pro adds three operators, three websites, 1,000 AI answers a month and the e-mail channel.

The Bottom Line

You can add live chat to Ruby on Rails in one partial, one render call and one production check. Keep the key in credentials, test Turbo navigation by clicking through a few pages and allow the script in your content security policy if you use one. Then put your time into teaching Mio from your own pages and documents. Start free with Talkmio, paste the snippet into your layout and see the bubble on your site today.


Try Talkmio on your site

Free plan, no card required.

Start free