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

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

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

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

Ruby on Rails

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

Q&A

解決済

2回答

3416閲覧

「http://localhost:3000/users」にアクセスして動作確認をした際に、接続画面に「Hello,world!」を表示させたい

wesker.alice

総合スコア45

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/03/08 11:57

編集2020/03/09 14:25

前提・実現したいこと

書籍「たった1日で基本が身に付くRuby on Rails超入門」の内容を読みながら進めている初学者です。
P48「03 ルーティング情報を設定しよう」の項目で、画面に「Hello,world!」を表示させたいのですが、Railsのデフォルトのトップページが表示され、「Hello,world!」の文字が見当たりません。
おそらく、ルーティング情報が正しく設定できていないのだと思うのですが、書籍の通りに記述しても「Hello,world!」が表示されず困っています。
何卒よろしくお願いいたします。

発生している問題・エラーメッセージ

http://localhost:3000/users」にアクセスした際にルーティング追加後の動作確認で「Hello,world!」が表示されない。

該当のソースコード

users_controller.rb

class UsersController < ApplicationController def index @hello = 'Hello, world!' render template: 'users/index' end end

routes.rb

Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html get '/users', to: 'users#index' get '/users/new' end

試したこと

書籍に記述されている内容を追記して保存した上で「http://localhost:3000/users」にアクセスして動作確認をした際に、「Hello,world!」が表示されない。(Railsのデフォルトのトップページが表示される)

補足情報(FW/ツールのバージョンなど)

MacOS Catalina ver10.15.3
Ruby ver2.6.3
Rails ver6.0.2.1
VS Code ver1.42.1

書籍の環境
windows 10 Pro
Ruby ver2.4.1
Ruby on Rails ver5.1.2

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

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

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

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

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

guest

回答2

0

ベストアンサー

別途コメントさせていただきましたが、ターミナルで対象プロジェクト直下に移動し、rails routes コマンドを打つと現在のルーティング設定が確認できます。

以下のような表示が出ていなければ、ルーティングが正しく設定されていない状態です。

users GET /users(.:format) users#index

また、手元の電子書籍版を確認したところ「03 ルーティング情報を設定しよう」のサンプルコードは以下のようになっていました。

users_controller.rb

class UsersController < ApplicationController def index render plain: 'Hello, world!' end end

routes.rb

Rails.application.routes.draw do # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html get '/users', to: 'users#index' end

実際にこの状態のサンプルプログラムを動かして「Hello,world!」の表示も確認することが出来ました。
イメージ説明

ご提示のソースコードを見ると、もう少し先の章まで進んでいるように見受けられますが、関連するファイルの準備が不足してる可能性はないでしょうか?

出版社の書籍ページからサンプルファイルをダウンロードすることもできますので、こちらと照らし合わせてみると、正しく動作しない原因がわかるかもしれません。
https://gihyo.jp/book/2018/978-4-7741-9618-3/support

投稿2020/03/11 16:55

unsoluble_sugar

総合スコア222

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

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

wesker.alice

2020/03/12 11:13

ご回答ありがとうございます。 ソースコードが先まで進んでいるのは、ひとまず分からずとも先に進んで回答が来たら戻って手直しをと考えていて、そのことも併せて記載すべきでした。 大変申し訳ありません。 混乱していて、もはやどこが間違っていてどこが正しいのか分からなくなってきたので、最初から作り直しました。 作り直した上で、rails routesを打つと下記の内容が表示されました。 Prefix Verb URI Pattern Controller#Action users GET /users(.:format) users#index 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 ーーーーーーーーーーーーーーーーーーーーーーーーーーー 「users GET /users(.:format) users#index」の一文も表記されていますが、rails sでサーバー起動後、「http://localhost:3000」にアクセスしてみるものの、デフォルト画面のままでHello,world!は表示されませんでした。 出版社のサンプルデータと照らし合わせてみます。
wesker.alice

2020/03/12 12:22

アクセスして表示させることができました! なぜアクセスして表示できないのか、よくよくみてみると 本来のURL→「http://localhost:3000/users」 私がアクセスしていたURL→「http://localhost:3000」 原因が「アクセスURLが間違っていた」でした。 お手数おかけいたしました。
guest

0

こういう場合はまずrails routesで確認ですね。ちゃんと/usersは生えてますか?

投稿2020/03/09 03:39

rox

総合スコア179

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

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

wesker.alice

2020/03/09 14:31

VSCode内では Users > ユーザーネーム名 > sample > config > routes.rb という流れで以下のroutes.rbを確認しています。 routes.rb ------------------------------------------------ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html get '/users', to: 'users#index' get '/users/new' end ------------------------------------------------ とは違う箇所を確認ということでしょうか? /usersが生えているかいないかだと、Usersから移行しているので生えているとなるのでしょうか?
wesker.alice

2020/03/09 14:39

追記すみません。 エクスプローラーの欄で 開いているエディター routes.rb 〜/sample/config となっており、/usersが生えていない状態のようにも思います。
unsoluble_sugar

2020/03/11 16:42

ターミナルで対象プロジェクト直下に移動し、rails routes コマンドを打ってみてください。 以下のような表示が出ていなければ、ルーティングが正しく設定されていない状態です。 users GET /users(.:format) users#index 一度 Ctrl-C でサーバーを停止してから、再度起動しても変わらない状態でしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問