0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Railsの実装まで①立ち上げ~画像処理編

Last updated at Posted at 2025-02-08

備忘録

アプリケーション作成からサーバー立ち上げ

まずはターミナルで以下コマンドを実行してアプリケーションを作成。
(XXXは任意のアプリケーション名)

$ rails new XXX

cdコマンドでアプリケーションのファイルに移動する。

$ cd XXX

これだけだとサーバーは立ち上がらない(省略)ので、おまじないのプラグインを実行。

$ yarn add @babel/plugin-proposal-private-methods @babel/plugin-proposal-private-property-in-object

これでサーバーを立ち上げることができる。

$ Rails s -p 8080

Topページ作成

Rails sをした際に表示されるページを定義して、表示されるようにする。

config/routes.rb
Rails.application.routes.draw do
root to: "homes#top"
end

ActiveStorageのインストール

画像を扱う場合ActiveStorageをインストールしておく。

$ rails active_storage:install

実行すると、db/migrate内にActiveStorageのマイグレーションファイルが作成されるので、マイグレート。

$ rails db:migrate

Rails側で画像サイズの変更を行う場合

CSS側とRailsで画像サイズを変更する2通りがありますが、ここではRails側でリサイズする場合の設定を行う。

まずはGemfileの記述をいじる。

# gem 'image_processing', '~>1.2'

から

gem 'image_processing', '~>1.2'

Gemfileをいじったらbundle installを実行。

$ bundle install

また、以下記述を加えます。

config/environments/development.rb
 config.active_job.queue_adapter = :inline

これで、Railsで画像のサイズ変更をする準備が完了。
画像は任意のものをVS code上のapp/assets/imagesに入れる。
※ActiveStorageは大き過ぎるファイルはエラーを吐く可能性があるので注意。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?