Unable to include a stylesheet link in my view - ruby-on-rails

I'm having a problem declaring a stylesheet tag in my Rails 5 view. I have this
<%= stylesheet_link_tag 'form', media: 'all', 'data-turbolinks-track': 'reload' %>
and then in my config/initializers/assets.rb I have this at the end
Rails.application.config.assets.precompile += %w( form.css )
My file is located at app/assets/stylesheets/form.css.scss, but even after restarting my server, I get the error below
Sprockets::Rails::Helper::AssetNotPrecompiled in MyEvents#index
Asset was not declared to be precompiled in production.
Add `Rails.application.config.assets.precompile += %w( form.css )` to `config/initializers/assets.rb` and restart your server
How do I get Rails to include this stylesheet on my page?

When I changed the stylesheet tag to simply
<%= stylesheet_link_tag 'form' %>
then eveerything worked.

Related

application.css file not being loaded from browser cache

Website is running on Puma production server.
Every time I go to another page, the application.css file is also downloaded every time. But the application.js is loaded from cache successfully.
From rails official document.
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => "reload" %>

The asset "devise/sessions.css" is not present in the asset pipeline

I'm trying configure the devise gem on my application built in Rails 5.2.
After install devise and create the views I'm get this error
The asset "devise/sessions.css" is not present in the asset pipeline.
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_link_tag params[:controller], media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag params[:controller], 'data-turbolinks-track': 'reload' %>
That was raised on line #9.
I've already created this file on my app/assets folder and setted on my assets.rb file like this:
Rails.application.config.assets.precompile += %w( addresses.css
welcome.css
donations.css
givers.css
sessions.css
scaffolds.css)
and this:
Rails.application.config.assets.precompile += %w( addresses.css
welcome.css
donations.css
givers.css
devise/sessions.css
scaffolds.css)
But I continue receiving the same error.
I have no more ideias what to do to solve my problem, someone could help me, please?

Rails 5: CSS will not link to html

I'm new to rails and trying to link main.css.scss to index.html.erb file in views/main.
My css file is located in app/assets/stylesheets. This is default, I did not change anything.
So in my index.html.erb file I added the following:
<%= stylesheet_link_tag "main" %>
This throwed an error which advised me to include ".css" at the end of "main". I added ".css" and ".css.scss" to the end of main. The main page loads, but without the css file.
What is the correct way to link css in rails 5?
Read the api docs here. It will help you to understand how you can link css in rails.
Also take a look at Samuel's answer. He explained it really well.
It's very simple. If you don't want to use default application.css and application.js, then create new files, like in your case main:
app/assets/stylesheets/main.scss
app/assets/javascripts/main.js
Then in your layout include those files:
<!DOCTYPE html>
<html>
<head>
<title>Foobar</title>
<%= stylesheet_link_tag 'main', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'main', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
And add those files to assets.rb:
Rails.application.config.assets.precompile += %w( main.scss main.js)
And restart the server.

Inline CSS from the asset pipeline

A fresh Rails application has the following code in app/views/layouts/application.html.erb to include a link to the - in production - minified CSS:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
For the reason of WebPerformance I'd like to use the asset pipeline to minify and organize my CSS but inline it in the header.
How can I tell Rails to inline the CSS of the asset pipeline in the application.html.erb?
This code will do the trick:
<style>
<% css = File.read("#{Rails.root}/public#{asset_path("application", type: :stylesheet)}") %>
<%= css.html_safe %>
</style>

Bootstrap Gem Installation Issue

I have added the bootstrap-sass gem and have installed the bundle. I have gone to github and think I have followed the directions correctly to finish the install for ruby on rails. It doesn't appear to be making a difference however on my local site - no font/formatting changes have been made, even after the install.
I am using a PC, https://github.com/findingtheway/pin
Change your app/views/layouts/application.html.erb to this:
<head>
<title>Pin</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
Rails uses application by default you have default which is why bootstrap is not loading.

Resources