LoginSignup
sone888h
@sone888h

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

devise_forのルーティングが記述されない(bin/rails db:migrate RAILS_ENV=developmentエラー)

ruby 3.1.4
rails 6.1.7.7
macOS Big Sur 11.7.10
docker 24.0.6

解決したいこと

Rails6アプリにて、docker環境でdeviseを導入しましたが、
docker-compose exec web rails g devise User
を行ってもdevise_forのルーティングが自動で追加されないため、何かアドバイスをいただきたく思い投稿いたしました。

docker-compose exec web rails g devise User
を行ったところ、

#   Migrations are pending. To resolve this issue, run:
#   
#           bin/rails db:migrate RAILS_ENV=development
#   
#   You have 1 pending migration:

と出てしまいます。
このマイグレートできていないファイルというのはrails g devise Userで作ったものなので問題ないかと思い、メッセージ通りbin/rails db:migrate RAILS_ENV=developmentを行っても、

devise_for
のルーティングが作成されません。

docker-compose exec web bundle install

docker-compose build

docker-compose exec web rails g devise:install

docker-compose exec web rails g devise User

docker-compose exec web bin/rails db:migrate RAILS_ENV=development

docker-compose exec web rails g devise:views User

docker-compose exec web rails g devise:controllers users

の順で行い、ルーティング以外は作成しているように思います。

スクリーンショット 2024-04-16 20.43.10.png
スクリーンショット 2024-04-16 20.43.36.png
スクリーンショット 2024-04-16 20.43.57.png

発生している問題・エラー

$ docker-compose exec web rails g devise User

Running via Spring preloader in process 70
      invoke  active_record
      create    db/migrate/20240414025949_devise_create_users.rb
      create    app/models/user.rb
      invoke    rspec
Running via Spring preloader in process 79
Migrations are pending. To resolve this issue, run:
        bin/rails db:migrate RAILS_ENV=development
You have 1 pending migration:
20240414025949_devise_create_users.rb
While loading rails_helper an `exit` / `raise SystemExit` occurred, RSpec will now quit.
Failure/Error: abort e.to_s.strip
SystemExit:
  Migrations are pending. To resolve this issue, run:
          bin/rails db:migrate RAILS_ENV=development
  You have 1 pending migration:
  20240414025949_devise_create_users.rb
略
# -e:1:in `<main>'
# ------------------
# --- Caused by: ---
# ActiveRecord::PendingMigrationError:
#   
#   
#   Migrations are pending. To resolve this issue, run:
#   
#           bin/rails db:migrate RAILS_ENV=development
#   
#   You have 1 pending migration:
#   
#   20240414025949_devise_create_users.rb
#   /usr/local/bundle/gems/activerecord-6.1.7.7/lib/active_record/migration.rb:625:in `check_pending!'

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

該当するソースコード

Dockerfile

FROM ruby:3.1.4-buster

RUN apt-get update -qq && apt-get install -y build-essential nodejs default-mysql-client libmariadb-dev-compat
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install Node.js 14
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y yarn

RUN mkdir /src
WORKDIR /src

COPY Gemfile /src/Gemfile
COPY Gemfile.lock /src/Gemfile.lock

RUN bundle install

COPY . /src

docker-compose.yml

docker-compose.yml
version: '3'

services:
  web:
    build:
      dockerfile: Dockerfile
      context: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    ports:
      - '3000:3000'
    volumes:
      - type: bind
        source: .
        target: /src
    depends_on:
      - db

  db:
    image: mysql:8.0
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: new_root_password
    volumes:
      - type: volume
        source: mysql
        target: /var/lib/mysql

  webpacker:
    build:
      context: .
    command: bundle exec bin/webpack-dev-server
    ports:
      - "8000:8000"
      
volumes:
  mysql:
  vendor_bundle:
  node_modules:
  storage:

Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    actioncable (6.1.7.7)
      actionpack (= 6.1.7.7)
      activesupport (= 6.1.7.7)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailbox (6.1.7.7)
      actionpack (= 6.1.7.7)
      activejob (= 6.1.7.7)
      activerecord (= 6.1.7.7)
      activestorage (= 6.1.7.7)
      activesupport (= 6.1.7.7)
      mail (>= 2.7.1)
    actionmailer (6.1.7.7)
      actionpack (= 6.1.7.7)
      actionview (= 6.1.7.7)
      activejob (= 6.1.7.7)
      activesupport (= 6.1.7.7)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (6.1.7.7)
      actionview (= 6.1.7.7)
      activesupport (= 6.1.7.7)
      rack (~> 2.0, >= 2.0.9)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.2.0)
    actiontext (6.1.7.7)
      actionpack (= 6.1.7.7)
      activerecord (= 6.1.7.7)
      activestorage (= 6.1.7.7)
      activesupport (= 6.1.7.7)
      nokogiri (>= 1.8.5)
    actionview (6.1.7.7)
      activesupport (= 6.1.7.7)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.1, >= 1.2.0)
    activejob (6.1.7.7)
      activesupport (= 6.1.7.7)
      globalid (>= 0.3.6)
    activemodel (6.1.7.7)
      activesupport (= 6.1.7.7)
    activerecord (6.1.7.7)
      activemodel (= 6.1.7.7)
      activesupport (= 6.1.7.7)
    activestorage (6.1.7.7)
      actionpack (= 6.1.7.7)
      activejob (= 6.1.7.7)
      activerecord (= 6.1.7.7)
      activesupport (= 6.1.7.7)
      marcel (~> 1.0)
      mini_mime (>= 1.1.0)
    activesupport (6.1.7.7)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      tzinfo (~> 2.0)
      zeitwerk (~> 2.3)
    addressable (2.8.6)
      public_suffix (>= 2.0.2, < 6.0)
    ast (2.4.2)
    bcrypt (3.1.20)
    bindex (0.8.1)
    bootsnap (1.18.3)
      msgpack (~> 1.2)
    builder (3.2.4)
    byebug (11.1.3)
    capybara (3.40.0)
      addressable
      matrix
      mini_mime (>= 0.1.3)
      nokogiri (~> 1.11)
      rack (>= 1.6.0)
      rack-test (>= 0.6.3)
      regexp_parser (>= 1.5, < 3.0)
      xpath (~> 3.2)
    concurrent-ruby (1.2.3)
    crass (1.0.6)
    date (3.3.4)
    devise (4.9.4)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0)
      responders
      warden (~> 1.2.3)
    diff-lcs (1.5.1)
    erubi (1.12.0)
    factory_bot (6.4.6)
      activesupport (>= 5.0.0)
    factory_bot_rails (6.4.3)
      factory_bot (~> 6.4)
      railties (>= 5.0.0)
    ffi (1.16.3)
    globalid (1.2.1)
      activesupport (>= 6.1)
    i18n (1.14.4)
      concurrent-ruby (~> 1.0)
    jbuilder (2.11.5)
      actionview (>= 5.0.0)
      activesupport (>= 5.0.0)
    json (2.7.2)
    listen (3.9.0)
      rb-fsevent (~> 0.10, >= 0.10.3)
      rb-inotify (~> 0.9, >= 0.9.10)
    loofah (2.22.0)
      crass (~> 1.0.2)
      nokogiri (>= 1.12.0)
    mail (2.8.1)
      mini_mime (>= 0.1.1)
      net-imap
      net-pop
      net-smtp
    marcel (1.0.4)
    matrix (0.4.2)
    method_source (1.0.0)
    mini_mime (1.1.5)
    minitest (5.22.3)
    msgpack (1.7.2)
    mysql2 (0.5.6)
    net-imap (0.4.10)
      date
      net-protocol
    net-pop (0.1.2)
      net-protocol
    net-protocol (0.2.2)
      timeout
    net-smtp (0.5.0)
      net-protocol
    nio4r (2.7.1)
    nokogiri (1.16.3-x86_64-linux)
      racc (~> 1.4)
    orm_adapter (0.5.0)
    parallel (1.24.0)
    parser (3.3.0.5)
      ast (~> 2.4.1)
      racc
    pg (1.5.6)
    public_suffix (5.0.5)
    puma (5.6.8)
      nio4r (~> 2.0)
    racc (1.7.3)
    rack (2.2.9)
    rack-mini-profiler (2.3.4)
      rack (>= 1.2.0)
    rack-proxy (0.7.7)
      rack
    rack-test (2.1.0)
      rack (>= 1.3)
    rails (6.1.7.7)
      actioncable (= 6.1.7.7)
      actionmailbox (= 6.1.7.7)
      actionmailer (= 6.1.7.7)
      actionpack (= 6.1.7.7)
      actiontext (= 6.1.7.7)
      actionview (= 6.1.7.7)
      activejob (= 6.1.7.7)
      activemodel (= 6.1.7.7)
      activerecord (= 6.1.7.7)
      activestorage (= 6.1.7.7)
      activesupport (= 6.1.7.7)
      bundler (>= 1.15.0)
      railties (= 6.1.7.7)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.2.0)
      activesupport (>= 5.0.0)
      minitest
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.6.0)
      loofah (~> 2.21)
      nokogiri (~> 1.14)
    railties (6.1.7.7)
      actionpack (= 6.1.7.7)
      activesupport (= 6.1.7.7)
      method_source
      rake (>= 12.2)
      thor (~> 1.0)
    rainbow (3.1.1)
    rake (13.2.1)
    rb-fsevent (0.11.2)
    rb-inotify (0.10.1)
      ffi (~> 1.0)
    regexp_parser (2.9.0)
    responders (3.1.1)
      actionpack (>= 5.2)
      railties (>= 5.2)
    rexml (3.2.6)
    rspec-core (3.13.0)
      rspec-support (~> 3.13.0)
    rspec-expectations (3.13.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.13.0)
    rspec-mocks (3.13.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.13.0)
    rspec-rails (6.1.2)
      actionpack (>= 6.1)
      activesupport (>= 6.1)
      railties (>= 6.1)
      rspec-core (~> 3.13)
      rspec-expectations (~> 3.13)
      rspec-mocks (~> 3.13)
      rspec-support (~> 3.13)
    rspec-support (3.13.1)
    rubocop (1.32.0)
      json (~> 2.3)
      parallel (~> 1.10)
      parser (>= 3.1.0.0)
      rainbow (>= 2.2.2, < 4.0)
      regexp_parser (>= 1.8, < 3.0)
      rexml (>= 3.2.5, < 4.0)
      rubocop-ast (>= 1.19.1, < 2.0)
      ruby-progressbar (~> 1.7)
      unicode-display_width (>= 1.4.0, < 3.0)
    rubocop-airbnb (6.0.0)
      rubocop (~> 1.32.0)
      rubocop-performance (~> 1.10.2)
      rubocop-rails (~> 2.9.1)
      rubocop-rspec (~> 2.0.0)
    rubocop-ast (1.31.2)
      parser (>= 3.3.0.4)
    rubocop-performance (1.10.2)
      rubocop (>= 0.90.0, < 2.0)
      rubocop-ast (>= 0.4.0)
    rubocop-rails (2.9.1)
      activesupport (>= 4.2.0)
      rack (>= 1.1)
      rubocop (>= 0.90.0, < 2.0)
    rubocop-rspec (2.0.1)
      rubocop (~> 1.0)
      rubocop-ast (>= 1.1.0)
    ruby-progressbar (1.13.0)
    rubyzip (2.3.2)
    sass-rails (6.0.0)
      sassc-rails (~> 2.1, >= 2.1.1)
    sassc (2.4.0)
      ffi (~> 1.9)
    sassc-rails (2.1.2)
      railties (>= 4.0.0)
      sassc (>= 2.0)
      sprockets (> 3.0)
      sprockets-rails
      tilt
    selenium-webdriver (4.10.0)
      rexml (~> 3.2, >= 3.2.5)
      rubyzip (>= 1.2.2, < 3.0)
      websocket (~> 1.0)
    semantic_range (3.0.0)
    spring (4.1.3)
    sprockets (4.2.1)
      concurrent-ruby (~> 1.0)
      rack (>= 2.2.4, < 4)
    sprockets-rails (3.4.2)
      actionpack (>= 5.2)
      activesupport (>= 5.2)
      sprockets (>= 3.0.0)
    thor (1.3.1)
    tilt (2.3.0)
    timeout (0.4.1)
    turbolinks (5.2.1)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (2.0.6)
      concurrent-ruby (~> 1.0)
    unicode-display_width (2.5.0)
    warden (1.2.9)
      rack (>= 2.0.9)
    web-console (4.2.1)
      actionview (>= 6.0.0)
      activemodel (>= 6.0.0)
      bindex (>= 0.4.0)
      railties (>= 6.0.0)
    webdrivers (5.3.1)
      nokogiri (~> 1.6)
      rubyzip (>= 1.3.0)
      selenium-webdriver (~> 4.0, < 4.11)
    webpacker (5.4.4)
      activesupport (>= 5.2)
      rack-proxy (>= 0.6.1)
      railties (>= 5.2)
      semantic_range (>= 2.3.0)
    websocket (1.2.10)
    websocket-driver (0.7.6)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.5)
    xpath (3.2.0)
      nokogiri (~> 1.8)
    zeitwerk (2.6.13)

PLATFORMS
  x86_64-linux

DEPENDENCIES
  bootsnap (>= 1.4.4)
  byebug
  capybara (>= 3.26)
  devise (~> 4.8, >= 4.8.1)
  factory_bot_rails
  jbuilder (~> 2.7)
  listen (~> 3.3)
  mysql2 (~> 0.5)
  pg
  puma (~> 5.0)
  rack-mini-profiler (~> 2.0)
  rails (~> 6.1.7, >= 6.1.7.7)
  rspec-rails
  rubocop-airbnb
  sass-rails (>= 6)
  selenium-webdriver (>= 4.0.0.rc1)
  spring
  turbolinks (~> 5)
  tzinfo-data
  web-console (>= 4.1.0)
  webdrivers
  webpacker (~> 5.0)

RUBY VERSION
   ruby 3.1.4p223

BUNDLED WITH
   2.4.10

routes.rb
Rails.application.routes.draw do
  devise_for :users #手動で足しましたが効かず

  root to: 'home#top'
  resources :diaries
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

自分で試したこと

・当初deviseを最新のバージョンにしていたので、4.8.1に変更しました。(4.9はRails7?)
そうしてやり直したのですが、うまくいかず詰まっています。
Gemfile.lcokにて、deviseが、specsは(4.9.4)に対し、DEPENDENCIES:は(~> 4.8, >= 4.8.1)になっているのもうまくいかない原因の一つだろうとは思っているのですが、bundle installやbuildをしてもここが変わりません。(4.8.xにしたい)
・また、手動でdevise_forを書き足してbundle install等しても変わりませんでした。

Dockerfileの設定がおかしいのでしょうか?検索しても同じ現象が起きている方がおらず、お手上げの状態です。

そもそも、docker-compose exec web rails g devise Userを行っていきなり

Migrations are pending. To resolve this issue, run:
        bin/rails db:migrate RAILS_ENV=development

が出てしまうのが問題だとは思っているのですが、なぜこうなってしまうのかが見当がつきません。dbのリセットなどすれば解消するものでしょうか?

補足)migrationの履歴
スクリーンショット 2024-04-16 20.33.28.png

初心者とはいえ知識不足で恐れいりますが、何かお気づきのことがありましたらアドバイスいただけますと幸いです。
よろしくお願いいたします。

0

1Answer

【自己解決】
こちら、
RAILS_ENV=test rails db:drop
RAILS_ENV=test rails db:create
RAILS_ENV=test rails db:migrate
を行ったらできました。
https://teratail.com/questions/229417

以前のマイグレーションがうまくいっていなかった?のかと思います。
大勢での開発だとあまりよろしくない方法かもしれませんが、個人作成のものですので、この方法を取りました。
ひとまずdeivseが作れてよかった…

0

Your answer might help someone💌