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

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

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

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

Ruby on Rails 6

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

Q&A

1回答

967閲覧

Railsのコントローラにおいてビュー作成ができない?(Completed 500 Internal Server Error in ~ が発生する)

yoangelo

総合スコア8

Ruby

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

Ruby on Rails 6

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

0グッド

0クリップ

投稿2020/02/25 01:14

編集2020/02/25 01:55

タイトルの通り、Rails においてコントローラTasksControllerを新規作成し、ビューとのルーティングを設定したのですが、localhost:3000にアクセスすると下記画像のエラーページが表示されます。
エラーページ
サーバーログを確認すると、

Started GET "/" for ::1 at 2020-02-25 02:20:28 +0900 Processing by TasksController#index as HTML Rendering tasks/index.html.slim within layouts/application Rendered tasks/index.html.slim within layouts/application (Duration: 8.1ms | Allocations: 1811) Completed 500 Internal Server Error in 52ms (ActiveRecord: 0.0ms | Allocations: 14428)

上記のように500番のサーバーエラーについて表示されるのみでエラーの根本的原因が掴めません。

この問題についての原因および対策についてご教示いただけないでしょうか。
#これまで行ったこと
0. rails newでアプリの新規作成

  1. Slimの導入
  2. Bootstrapの導入
  3. Rails日本語化
  4. Taskモデルの作成
  5. TasksControllerの作成およびルーティングの設定(エラー発生)

特に目新しいこともしておらず、最初のビュー確認でコケた、といった感じです。

#試したこと・確認したこと
##ルーティングの設定の確認
$ rails routesを実行すると下記の通りルーティングの設定が表示されるのでルーティングは正しく設定されていることを確認しました。

$ rails routes Prefix Verb URI Pattern Controller#Action root GET / tasks#index tasks GET /tasks(.:format) tasks#index POST /tasks(.:format) tasks#create new_task GET /tasks/new(.:format) tasks#new edit_task GET /tasks/:id/edit(.:format) tasks#edit task GET /tasks/:id(.:format) tasks#show PATCH /tasks/:id(.:format) tasks#update PUT /tasks/:id(.:format) tasks#update DELETE /tasks/:id(.:format) tasks#destroy rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create

##作成コントローラにおける他アクションのビューの表示確認
作成したTasksControllerにおいて他のアクション(new,edit,show)のビューをURLに入力して確認しましたが、同じブラウザ画面が表示され、ログも同様のエラーが出力されました。

Started GET "//tasks/new" for ::1 at 2020-02-25 09:21:27 +0900 Processing by TasksController#new as HTML Rendering tasks/new.html.slim within layouts/application Rendered tasks/new.html.slim within layouts/application (Duration: 10.7ms | Allocations: 1803) Completed 500 Internal Server Error in 38ms (ActiveRecord: 0.0ms | Allocations: 14043) Started GET "/tasks/1/edit" for ::1 at 2020-02-25 09:21:54 +0900 Processing by TasksController#edit as HTML Parameters: {"id"=>"1"} Rendering tasks/edit.html.slim within layouts/application Rendered tasks/edit.html.slim within layouts/application (Duration: 4.5ms | Allocations: 1807) Completed 500 Internal Server Error in 30ms (ActiveRecord: 0.0ms | Allocations: 14051) Started GET "/tasks/1" for ::1 at 2020-02-25 09:22:09 +0900 Processing by TasksController#show as HTML Parameters: {"id"=>"1"} Rendering tasks/show.html.slim within layouts/application Rendered tasks/show.html.slim within layouts/application (Duration: 8.3ms | Allocations: 1802) Completed 500 Internal Server Error in 37ms (ActiveRecord: 0.0ms | Allocations: 14017)

##作成コントローラ以外のビューの表示確認
Railsに最初から設定されているURL(/rails/conductor/action_mailbox/inbound_emails/new)にアクセスしたところDeliver new inbound emailのページが表示されたので、Rails本体?の動作は正しそうです。
Deliver new inbound email

##作成モデルの動作確認
作成したTaskモデルをRailsコンソール上にて新規作成したところ、newの後正しくsaveされたのでモデルの動作およびDBも問題はなさそうです。

$ rails c Running via Spring preloader in process 8442 Loading development environment (Rails 6.0.2.1) >> task = Task.new(name:"TEST",description:"テスト") => #<Task id: nil, name: "TEST", description: "テスト", created_at: nil, updated_at: nil> >> task.save (0.2ms) BEGIN Task Create (45.5ms) INSERT INTO "tasks" ("name", "description", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "TEST"], ["description", "テスト"], ["created _at", "2020-02-25 00:40:07.755705"], ["updated_at", "2020-02-25 00:40:07.755705"]] (33.1ms) COMMIT => true >> task => #<Task id: 2, name: "TEST", description: "テスト", created_at: "2020-02-25 00:40:07", updated_at: "2020-02-25 00:40:07">

#考察(というよりわからないこと)
上記、試したことの結果からcontroller側に何か原因がありそう…とは思うのですが、エラー内容が500番のみで手がかりが少なくブラックボックスであるため、これ以上の推測は難しく、もうお手上げ、といった感じです????

何卒、お知恵を拝借できないでしょうか…。

#開発環境
OS : macOS Mojave Ver.10.14.6
Ruby : Ver.2.6.5p114
Rails : Ver.6.0.2.1
DB : postgresql

#ソースコード

rb

1【routes.rb】 2 3 4Rails.application.routes.draw do 5 root to: 'tasks#index' 6 resources :tasks 7end 8

rb

1【tasks_controller.rb】 2 3 4class TasksController < ApplicationController 5 def index 6 end 7 8 def show 9 end 10 11 def new 12 end 13 14 def edit 15 end 16end 17

slim

1【application.html.slim】 2 3 4doctype html 5html 6 head 7 title 8 | Taskle 9 = csrf_meta_tags 10 = csp_meta_tag 11 = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' 12 = javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' 13 body 14 .app-title.navbar.navbar-expand-md.navbar-light.bg-light 15 .navbar-brand Taskle 16 .container 17 = yield

rb

1【db/schema.rb】 2 3 4ActiveRecord::Schema.define(version: 2020_02_24_144931) do 5 6 # These are extensions that must be enabled in order to support this database 7 enable_extension "plpgsql" 8 9 create_table "tasks", force: :cascade do |t| 10 t.string "name" 11 t.text "description" 12 t.datetime "created_at", precision: 6, null: false 13 t.datetime "updated_at", precision: 6, null: false 14 end 15 16end

rb

1【config/environments/development.rb】 2 3Rails.application.configure do 4〜略〜 5 6# Show full error reports. 7config.consider_all_requests_local = true 8 9〜略〜 10

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

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

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

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

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

maisumakun

2020/02/25 01:33

まずは、config.consider_all_requests_local = trueを設定して、エラー表示させるところから始めてみましょう。
yoangelo

2020/02/25 01:56 編集

ご回答ありがとうございます! ご教示頂いた設定ですが、最初から以下のように設定されていました。 この度のエラーは開発環境で起こっているのですが、config.consider_all_requests_local = true としてもエラーの詳細表示がされないようです…。 【config/environments/development.rb】 Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development # since you don't have to restart the web server when you make code changes. config.cache_classes = false # Do not eager load code on boot. config.eager_load = false # Show full error reports. config.consider_all_requests_local = true ~略~
maisumakun

2020/02/25 01:56

間違えてproductionモードで動かしている、なんてことはありませんか?
yoangelo

2020/02/25 02:00 編集

ありがとうございます。 $ rails s すると、以下のようにサーバーから出力されるため、developmentモードで動いていると思われます…。 => Booting Puma => Rails 6.0.2.1 application starting in development => Run `rails server --help` for more startup options Puma starting in single mode... * Version 4.3.1 (ruby 2.6.5-p114), codename: Mysterious Traveller * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://127.0.0.1:3000 * Listening on tcp://[::1]:3000 Use Ctrl-C to stop
mongolia

2020/02/25 05:09

>TasksControllerの作成およびルーティングの設定(エラー発生) railsコマンドで作成していますか?その場合、コマンドはありますか? ソースコードにindexのviewがないので念の為確認ですが、viewファイルは作成済みでしょうか?
yoangelo

2020/02/26 13:55

ご回答ありがとうございます。返信が遅くなりすみません。 >railsコマンドで作成していますか?その場合、コマンドはありますか? $ rails g controller tasks index show new edit こちらのコマンドで一通りのCRUDアクションを同時生成しました。 その際に出力されたログは残っていませんが、特に問題なく生成されたと記憶しています。 なお、表示しようとしているviewは初期状態のindex.html.slimです。 【index.html.slim】 h1 Tasks#index p Find me in app/views/tasks/index.html.slim
guest

回答1

0

可能性は2つあります。
TasksController にactionは定義されているものの、中身はゼロです、ので

可能性1)viewが要求しているインスタンス変数がないためのエラー
可能性2)viewが作られていない

だが、、、
可能性2は、viewがないというエラーになると思うので1の方が可能性大きい
いずれにせよ
viewのcodeを載せて頂くのが必要でs。

投稿2020/02/25 11:46

winterboum

総合スコア23376

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

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

yoangelo

2020/02/26 13:49

ご回答ありがとうございます。返事が遅くなりすみません! 表示しようとしているファイルはこちらになります。 見ての通り、ただの初期状態です。要求している変数やビューなど何もありません…。 【index.html.slim】 h1 Tasks#index p Find me in app/views/tasks/index.html.slim
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問