ojfubd / storyshare

0 stars 0 forks source link

仮トップページを用意する #13

Closed ojfubd closed 3 months ago

ojfubd commented 3 months ago

デプロイでトップページが閲覧できるか確認できるように以下を行います。

具体的には、トップページ用のルーティング・コントローラー・アクション・ビューを用意します。

ルーティング:resources :tops, only: [:index] コントローラー:tops_controller アクション:index ビュー:app/views/tops/index.html.erb

コントローラーを生成しよう: rails g controller Tops index

以下のようなファイルが生成されるダナ:

ルーティング:config/routes.rb に get 'tops/index' が追加される。 コントローラー:app/controllers/tops_controller.rb ビュー:app/views/tops/index.html.erb

Prefix:

tops は、このルートに対応する名前付きルートのプレフィックスダ。tops_pathやtops_urlのように使うことができるんダ。 Verb:

GET は、HTTPメソッドの一つで、リソースを取得するためのリクエストを表しているダナ。他にもPOST、PUT、DELETEなどがあるゾ。 URI Pattern:

/tops(.:format) は、このルートにマッチするURLパターンダ。(.:format)はオプションで、レスポンスのフォーマット(例えば.jsonや.htmlなど)を指定することができるナ。 Controller#Action:

tops#index は、このルートがマッチしたときに呼び出されるコントローラーとアクションを表しているダナ。ここでは、TopsControllerのindexアクションが実行されるんダ。

●ビューの中身

Hello Rails World!!

Wellcome!!

# ActiveRecord::ConnectionNotEstablished (connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket? ): 解決策 PostgreSQLサーバーが起動していない: サーバーが起動しているか確認しよう。ターミナルで以下のコマンドを実行して、PostgreSQL PostgreSQLを起動するには、以下のコマンドを使ってみてナ: brew services start postgresql We could not find your database: storyshare_development. Available database configurations can be found in config/database.yml. To resolve this error: - Did you not create the database, or did you delete it? To create the database, run: bin/rails db:create - Has the database name changed? Verify that config/database.yml contains the correct database name. 解決策 このエラーメッセージは、Railsアプリケーションが指定されたデータベース(storyshare_development)を見つけられなかったことを示しているダナ。具体的には以下のことが考えられるゾ: データベースが作成されていない: データベースを作成していない場合、以下のコマンドを実行してデータベースを作成するんダナ。 bin/rails db:create データベースが削除された: データベースが何らかの理由で削除されてしまった場合も、上記のコマンドで再作成することができるゾ。 データベース名が変更された: config/database.ymlファイルの設定を確認して、正しいデータベース名が指定されていることを確認するんダナ。例えば、次のような設定が必要だゾ: development: adapter: postgresql database: storyshare_development ... Sprockets::Rails::Helper::AssetNotFound in Tops#index Showing /Users/okmurokouki/workspace/storyshare/app/views/layouts/application.html.erb where line #9 raised: The asset "application.css" is not present in the asset pipeline. Railsのアセットパイプラインが指定されたCSSファイル(application.css)を見つけられないことを示している touch app/assets/stylesheets/application.css エラー Sprockets::Rails::Helper::AssetNotPrecompiledError in Tops#index Showing /Users/okmurokouki/workspace/storyshare/app/views/layouts/application.html.erb where line #9 raised: Asset `application.css` was not declared to be precompiled in production. Declare links to your assets in `app/assets/config/manifest.js`. 解決策 これは、application.cssがプロダクション環境でプリコンパイルされていないことを示している pp/assets/config/manifest.jsを編集する: manifest.jsにapplication.cssをリンクとして追加するんダナ。以下のように記述する: //= link application.css アセットをプリコンパイルする: manifest.jsを編集した後、アセットをプリコンパイルする必要があるダナ。以下のコマンドを実行する: bin/rails assets:precompile エラー bin/rails assets:precompile yarn install v1.22.22 [1/4] 🔍 Resolving packages... success Already up-to-date. ✨ Done in 0.07s. yarn run v1.22.22 error Command "build" not found. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. bin/rails aborted! jsbundling-rails: Command build failed, ensure `yarn build` runs without errors Tasks: TOP => assets:precompile => javascript:build (See full trace by running task with --trace) 解決策 「assets:precompile」タスクが実行される際に、JavaScriptのビルドが失敗していることを示しているダナ。具体的には「yarn build」コマンドが見つからないというエラーだナ。 以下の手順で問題を解決することを試してみるといいカモしれないゾ: Yarnのインストールを確認: まず、Yarnが正しくインストールされているか確認しよう。 yarn -v パッケージのインストール: 必要なパッケージがインストールされているか確認するために、再度インストールを試してみる。 yarn install package.jsonの確認: プロジェクトのルートディレクトリにある package.json ファイルを確認して、「build」スクリプトが定義されているかチェックするんダ。 { "scripts": { "build": "webpack --config webpack.config.js" } } もし「build」スクリプトがない場合は、上記のように追加してみてダナ。 Webpackの設定: webpack.config.js ファイルが正しく設定されているか確認する。設定ファイルがない場合は、公式ドキュメントを参考にして設定を追加しよう。 yarn buildの実行: 最後に手動で「yarn build」コマンドを実行して、問題がないか確認してみよう。 yarn build サーバーを再起動する: 変更を反映させるために、サーバーを再起動するんダナ。 bin/rails server