Resolve stylesheet_link_tag fails to work in Ruby on Rails

Summary

Ruby on Rails developers might encounter a "TypeError" when stylesheet_link_tag fails, signaling problems locating assets in app/assets. To resolve this, add gem 'coffee-script-source', '1.8.0' to your Gemfile, run bundle install, then bundle update coffee-script-source, and restart the Rails server. While a quick workaround involves changing the stylesheet reference from "application" to "default", this only bypasses the error and does not fix the underlying asset loading issue, resulting in an application without its expected styles and scripts.

When developing Ruby On Rails application, developers sometimes will see an unexpected behavior where the application cannot be loaded due to "TypeError: Object doesn't support this property or method" when invoking stylesheet_link_tag. The symptom looks like

 And the Rails log will show something similar to below

The error occurs because there is some problem finding the correct assets which are located in app/assets. To resolve the issue, you can following below steps.

  1. Add gem 'coffee-script-source', '1.8.0' into Gemfile and run bundle install then
  2. Run bundle update coffee-script-source
  3. Restart rails server

Or there is a dirty workaround which is to change the code from

<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>

to

<%= stylesheet_link_tag "default", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "default", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>

But note this workaround doesn't really resolve the application issue, it just makes your application works without expected stylesheets and javascripts.

RUBY ON RAILS STYLESHEET_LINK_TAG JAVASCRIPT_INCLUDE_TAG TYPEERROR

  RELATED

  COMMENTS

2
AGT
Nov 12, 2018 at 12:12 am

thank you so much, it fixed my problem. I owe you a coffee(1.8.0') now :))))

AGT
Nov 12, 2018 at 12:14 am

thank you so much, it fixed my problem. I owe you a coffee(1.8.0') now :))))