JavaScript异常致Rails link_to功能失效,寻求解决方案
Hey there! Let's work through this issue together—it's a super common snag when combining Turbolinks and AJAX in Rails, so you're definitely not alone here. Here are the most likely fixes to get your links working again without ditching that critical javascript_include_tag:
1. Fix Event Binding for Turbolinks
Turbolinks replaces page content without full page reloads, which means traditional $(document).ready() only runs once (on the first page load). If your follow/unfollow AJAX code uses ready, it won't bind to buttons on subsequent pages—and worse, might leave behind event handlers that interfere with normal link behavior.
Replace your document ready code with Turbolinks' load event:
// Instead of $(document).ready(function() { ... }) $(document).on('turbolinks:load', function() { // Your follow/unfollow AJAX handler here $('.follow-btn').on('click', function(e) { e.preventDefault(); // Your AJAX logic ($.ajax or Rails UJS calls) }); });
This ensures your handler binds every time Turbolinks loads a new page.
2. Check for Overly Broad Event Listeners
If you accidentally attached a preventDefault() to all <a> tags (instead of just your follow buttons), that would block every link_to from navigating. Double-check your custom JS for something like:
// ❌ This breaks ALL links! $('a').on('click', function(e) { e.preventDefault(); // ... rest of code });
Fix this by targeting only your follow/unfollow buttons with a specific class (like follow-btn or unfollow-btn):
// ✅ Only targets your action buttons $('.follow-btn, .unfollow-btn').on('click', function(e) { e.preventDefault(); // AJAX logic here });
3. Verify Asset Pipeline Load Order
In app/assets/javascripts/application.js, make sure your dependencies are loaded in the correct order. A common mistake is loading Turbolinks before Rails UJS, which can cause conflicts:
// Correct order //= require rails-ujs //= require turbolinks //= require_tree .
This ensures Rails UJS (which handles remote: true links) loads before Turbolinks, preventing race conditions.
4. Dig for Hidden Console Errors
Even if you don't see visible errors, your browser's dev tools might have clues. Open the Console tab in Chrome/Firefox DevTools, refresh the page, then click a broken link. Look for:
- Uncaught reference errors (e.g.,
$ is not definedif jQuery isn't loaded) - Turbolinks-related warnings (e.g., about missing elements)
- AJAX request failures (check the Network tab for failed XHR calls)
5. Ensure remote: true Is Only on Follow Buttons
Double-check that you didn't accidentally add remote: true to other link_to calls. If a regular navigation link has remote: true, Rails will try to handle it via AJAX instead of normal navigation—without a corresponding JS handler, it'll fail silently.
Your follow/unfollow link should look like this:
<%= link_to "Follow", follow_path(user), remote: true, class: "follow-btn" %>
And regular navigation links should omit remote: true:
<%= link_to "Home", root_path %>
Give these steps a try—chances are one of them will fix your issue. Let me know if you need to dive deeper into any of these!
内容的提问来源于stack exchange,提问作者Kane Walsh

