railsでcssが反映されないエラーActionController::RoutingError (No route matches [GET] "/stylesheets"):style


app/assets/stylesheets/home.scss
上記のようにhome.scssファイルを作ってcssを書き

app/assets/views/home/index.html.erb
上記のようにhomeフォルダにindex.html.erbファイルを作ってhtmlを書いた。

「rails s」でサーバーを起動すると

ActionController::RoutingError (No route matches [GET] "/stylesheets")

のエラーが起きてしまった。

routes.rb
Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  get "index" =>"home#index"
  get "myactive" => "home#myactive"
  get "myhobby" => "home#myhobby"
  get "myskill" => "home#myskill"
end

ルーティングを確認したが特に問題はなさそう。

home.controller.rb
class HomeController < ActionController::Base
    def index
    end

    def myactive
    end

    def myhobby
    end

    def myskill
    end
end

上の「ActionController::Base」を「ApplicationController」に変更すると上手くいった。

これで理解したこと
・home.controller.rbとはviews/homeフォルダをコントロールするもので
 routes.rbでは「home#index」のようにする。home.scssに書く。
 home.controller.rbなど新しくファイルを作った場合は、親クラスをApplicationControllerにする。

・わざわざhome.controller.rbなど新しくファイルを作らなくても、views/applicationフォルダを作り、
 既存のapplication.cssにコードを書くとcssを反映することができる。