質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1098閲覧

Rails5で動いたコードがRails6で動かなくなった

Shmupeiii

総合スコア105

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2022/02/28 13:43

編集2022/02/28 13:45

Rails5.2で動かしていたRails環境がローカルだとエラーになってしまったためRails6に移行しました。
元のheroku
https://rails-docker-portfolio.herokuapp.com/
ログインid sample123
ログインパスワード sample12345
元のgithub
https://github.com/takoyan33/rails-docker

しかし、Rails6で動かすとこのようなエラーが出るようになりました。(その他の部分は動いています。)
掲示板作成部分
イメージ説明

そのため、解決法が分かる方がいれば教えて頂きたいです。

Ruby

1docker log 2| Started POST "/boards" for 172.20.0.1 at 2022-02-28 13:33:08 +0000 3rails-docker-2-web-1 | Cannot render console from 172.20.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1 4rails-docker-2-web-1 | Processing by BoardsController#create as HTML 5rails-docker-2-web-1 | Parameters: 6rails-docker-2-web-1 | { 7rails-docker-2-web-1 | "authenticity_token" => "[FILTERED]", 8rails-docker-2-web-1 | "board" => { 9rails-docker-2-web-1 | "title" => "サンプル", 10rails-docker-2-web-1 | "name" => "サンプル", 11rails-docker-2-web-1 | "body" => "サンプル", 12rails-docker-2-web-1 | "tag_ids" => [ 13rails-docker-2-web-1 | [0] "" 14rails-docker-2-web-1 | ] 15rails-docker-2-web-1 | }, 16rails-docker-2-web-1 | "commit" => "保存" 17rails-docker-2-web-1 | } 18rails-docker-2-web-1 | User Load (1.3ms) 19rails-docker-2-web-1 | SELECT 20rails-docker-2-web-1 | `users`. * 21rails-docker-2-web-1 | FROM 22rails-docker-2-web-1 | `users` 23rails-docker-2-web-1 | WHERE 24rails-docker-2-web-1 | `users`.`id` = 1 LIMIT 1 25rails-docker-2-web-1 | ↳ app/controllers/application_controller.rb:9:in `current_user' 26rails-docker-2-web-1 | Redirected to http://localhost:3000/boards/new 27rails-docker-2-web-1 | Completed 302 Found in 26ms (ActiveRecord: 3.2ms | Allocations: 24197) 28rails-docker-2-web-1 | 29rails-docker-2-web-1 | 30rails-docker-2-web-1 | Started GET "/boards/new" for 172.20.0.1 at 2022-02-28 13:33:08 +0000 31rails-docker-2-web-1 | Cannot render console from 172.20.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1 32rails-docker-2-web-1 | Processing by BoardsController#new as HTML 33rails-docker-2-web-1 | User Load (0.9ms) 34rails-docker-2-web-1 | SELECT 35rails-docker-2-web-1 | `users`. * 36rails-docker-2-web-1 | FROM 37rails-docker-2-web-1 | `users` 38rails-docker-2-web-1 | WHERE 39rails-docker-2-web-1 | `users`.`id` = 1 LIMIT 1 40rails-docker-2-web-1 | ↳ app/controllers/application_controller.rb:9:in `current_user' 41rails-docker-2-web-1 | Rendering layout layouts/application.html.erb 42rails-docker-2-web-1 | Rendering boards/new.html.erb within layouts/application 43rails-docker-2-web-1 | Rendered shared/_error_messages.html.erb (Duration: 0.1ms | Allocations: 42) 44rails-docker-2-web-1 | Tag Load (1.4ms)

Ruby

1#views/boards/_form.html.erb 2<%= render 'shared/error_messages' %> 3<%= form_with model: board do |f| %> 4 <div class="form-group"> 5 <%= f.label :title, 'タイトル' %> 6 <%= f.text_field :title, class: 'form-control' %> 7 </div> 8 <div class="form-group"> 9 <%= f.label :name, '名前' %> 10 <%= f.text_field :name, class: 'form-control' %> 11 </div> 12 <div class="form-group"> 13 <%= f.label :body, '本文' %> 14 <%= f.text_area :body, class: 'form-control', rows: 10 %> 15 </div> 16 <span class="image"> 17 <%= f.file_field :image, accept: "image/jpeg,image/gif,image/png" %> 18 </span> 19 <div class="form-group"> 20 <span>タグ</span> 21 <%= f.collection_check_boxes(:tag_ids, Tag.all, :id, :name) do |tag| %> 22 <div class="form-check"> 23 <%= tag.label class: 'form-check-label' do %> 24 <%= tag.check_box class: 'form-check-input' %> 25 <%= tag.text %> 26 <% end %> 27 </div> 28 <% end %> 29 </div> 30 <%= f.submit '保存', class: 'btn btn-primary' %> 31<% end %> 32 33<script type="text/javascript"> 34 $("#board_image").bind("change", function() { 35 var size_in_megabytes = this.files[0].size/1024/1024; 36 if (size_in_megabytes > 5) { 37 alert("Maximum file size is 5MB. Please choose a smaller file."); 38 $("#board_image").val(""); 39 } 40 }); 41</script> 42

Ruby

1#boards_controller 2 3class BoardsController < ApplicationController 4 before_action :set_target_board, only: %i[show edit update destroy] 5 6 def index 7 @boards = params[:tag_id].present? ? Tag.find(params[:tag_id]).boards : Board.all.order(id: "DESC") 8 @boards = @boards.page(params[:page]) 9 @boards_count = Board.all.count 10 @today_count = Board.created_today.count 11 @lastmonth_count = Board.created_last_month.count 12 @month_count = Board.created_month.count 13 end 14 15 def new 16 @board = Board.new(flash[:board]) 17 end 18 19 def create 20 board = Board.new(board_params) 21 board.image.attach(params[:board][:image]) 22 if board.save 23 flash[:notice] = "「#{board.title}」の掲示板を作成しました" 24 redirect_to board 25 else 26 redirect_back fallback_location: root_path, flash: { 27 board: board, 28 error_messages: board.errors.full_messages 29 } 30 end 31 end 32 33 def show 34 @comment = Comment.new(board_id: @board.id) 35 end 36 37 def edit 38 @board.attributes = flash[:board] if flash[:board] 39 end 40 41 def update 42 if @board.update(board_params) 43 redirect_to @board 44 else 45 redirect_back fallback_location: root_path , flash: { 46 board: @board, 47 error_messages: @board.errors.full_messages 48 } 49 end 50 end 51 52 def destroy 53 @board.destroy 54 redirect_to boards_path, flash: { notice: "「#{@board.title}」の掲示板が削除されました" } 55 end 56 57 private 58 59 def board_params 60 params.require(:board).permit(:content, :image, :name, :title, :body, tag_ids: []) 61 end 62 63 def set_target_board 64 @board = Board.find(params[:id]) 65 end 66end

Ruby

1#model/boards.rb 2class Board < ApplicationRecord 3 has_many :comments, dependent: :delete_all 4 has_many :board_tag_relations, dependent: :delete_all 5 has_many :tags, through: :board_tag_relations 6 has_one_attached :image 7 belongs_to :user 8 9 validates :name, length: { maximum: 10 } 10 validates :title, presence: true, length: { maximum: 30 } 11 validates :body, presence: true, length: { maximum: 1000 } 12 13 scope :created_today, -> { where(created_at: Time.zone.now.all_day) } # 今日 14 scope :created_last_month, -> { where(created_at: Time.zone.now.prev_month.all_day) } # 1ヶ月前の投稿 15 scope :created_month, -> { where(created_at: Time.zone.now.all_month) } # 今月の投稿 16 17 validates :image, content_type: { in: %w[image/jpeg image/gif image/png], 18 message: "must be a valid image format" }, 19 size: { less_than: 5.megabytes, 20 message: "should be less than 5MB" } 21 22 # 表示用のリサイズ済み画像を返す 23 def display_image 24 image.variant(resize_to_limit: [500, 500]) 25 end 26end

現状github

Ruby

1#Gemfile 2 3source 'https://rubygems.org' 4git_source(:github) { |repo| "https://github.com/#{repo}.git" } 5 6ruby '2.7.5' 7 8# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' 9gem 'rails', '~> 6.1.4', '>= 6.1.4.6' 10gem 'active_storage_validations', '0.8.2' 11# Use mysql as the database for Active Record 12gem 'mysql2', '~> 0.5' 13# Use Puma as the app server 14gem 'puma', '~> 5.0' 15# Use SCSS for stylesheets 16gem 'sass-rails', '>= 6' 17# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 18gem 'webpacker', '~> 5.0' 19# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 20gem 'turbolinks', '~> 5' 21# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 22gem 'jbuilder', '~> 2.7' 23# Use Redis adapter to run Action Cable in production 24# gem 'redis', '~> 4.0' 25# Use Active Model has_secure_password 26gem 'bcrypt', '~> 3.1.7' 27 28# Use Active Storage variant 29# gem 'image_processing', '~> 1.2' 30 31# Reduces boot times through caching; required in config/boot.rb 32gem 'bootstrap', '~>4.0.0' 33gem 'kaminari' 34gem 'rails-i18n' 35gem 'jquery-rails' 36 37group :development, :test do 38 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 39 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 40 gem 'rails-flog', require: 'flog' 41 gem 'rspec-rails', '~> 3.8' 42 gem 'rails-controller-testing' 43end 44 45group :development do 46 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 47 gem 'web-console', '>= 4.1.0' 48 # Display performance information such as SQL time and flame graphs for each request in your browser. 49 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 50 gem 'rack-mini-profiler', '~> 2.0' 51 gem 'listen', '~> 3.3' 52 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 53 gem 'spring' 54end 55 56group :test do 57 # Adds support for Capybara system testing and selenium driver 58 gem 'capybara', '>= 3.26' 59 gem 'selenium-webdriver' 60 # Easy installation and use of web drivers to run system tests with browsers 61 gem 'webdrivers' 62end 63 64# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 65gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 66

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Rails 6 では belongs_to :user とすると自動的に validates :user. presence: true がつくようになります。
このため「実在する」User のid がuser_id に入っていないと 表記のエラーとなります。

Board に userが 必須ではないのなら、 belongs_to :user , optional: true としてください

投稿2022/03/02 14:21

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Shmupeiii

2022/03/02 14:42

回答ありがとうございます。 勉強になりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問