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

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

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

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

Ruby on Rails 6

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

Q&A

解決済

1回答

339閲覧

rails新しいモデル(ユーザー)をつくるも、表示されない。

hurousyotoku500

総合スコア27

Ruby

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

Ruby on Rails 6

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

0グッド

0クリップ

投稿2020/09/01 11:48

編集2020/09/01 12:40

rails 初心者です。

現在、簡単なゴルフスコア入力アプリをrailsにて制作中で、ユーザー機能を付け加えているところでした。
セオリーどおり、モデルをつくって、コントローラ、ビューを設定して、ブラウザで〇〇/usersにアクセスしましたが、トップページと全く同じ表示になってしました。
実現させたいのは、ユーザーインデックスを表示させたいです。
Userインスタンスはいくつか作ってあります。

ルーティングが怪しいかなと思ってみましたが、

users GET /users(.:format)

の表示があったので、ここは問題ないのかなと思います。
エラーが出てくれないので、どこが悪いのかわからない状態です。
アドバイス、ヒントいただけないでしょうか
宜しくお願い致します。

routes.rb

RUBY

1Rails.application.routes.draw do 2 3 root to: 'scores#index' 4 resources :scores 5 6 get 'signup', to: 'users#new' 7 resources :users, only: [:index, :show, :new, :create] 8end

user.rb

ruby

1class User < ApplicationRecord 2 before_save { self.email.downcase! } 3 validates :name, presence: true, length: { maximum: 50 } 4 validates :email, presence: true, length: { maximum: 255 }, 5 format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i }, 6 uniqueness: { case_sensitive: false } 7 has_secure_password 8end

users_controller.rb

ruby

1class UsersController < ApplicationController 2 def index 3 @users = User.order(id: :desc).page(params[:page]).per(25) 4 end 5 6 def show 7 end 8 9 def new 10 end 11 12 def create 13 end 14end 15

users/index.html.erb

erb

1<%= render 'users', users: @users %>

users/_users.html.erb

erb

1<% if users.any? %> 2 <ul class="list-unstyled"> 3 <% users.each do |user| %> 4 <li class="media"> 5 <img class="mr-2 rounded" src="<%= gravatar_url(user, { size: 50 }) %>" alt=""> 6 <div class="media-body"> 7 <div> 8 <%= user.name %> 9 </div> 10 <div> 11 <p><%= link_to 'View profile', user_path(user) %></p> 12 </div> 13 </div> 14 </li> 15 <% end %> 16 </ul> 17 <%= paginate users %> 18<% end %>

ruby

1module UsersHelper 2 def gravatar_url(user, options = { size: 80 }) 3 gravatar_id = Digest::MD5::hexdigest(user.email.downcase) 4 size = options[:size] 5 "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" 6 end 7end 8

scores/index.html.etb

erb

1<h1>スコアカード</h1> 2 3<ul> 4 <% @scores.each do |score| %> 5 <li><%= score.id %> H: Par<%= score.par %> <%= link_to score.hole_score , score %> <%= score.hole_score - score.par %> </li> 6 7 <% end %> 8</ul> 9<p>今日のスコアは 10 <%= @total_score %>でした 11</p> 12 13<div class="center jumbotron"> 14 <div class="text-center"> 15 <h1>Welcome to the Microposts</h1> 16 <%= link_to 'Sign up now!', signup_path, class: 'btn btn-lg btn-primary' %> 17 </div> 18</div>

scores_controller.rb

ruby

1class ScoresController < ApplicationController 2 def index 3 @scores = Score.all 4 @total_score =Score.all.sum(:hole_score) 5 6 end 7 def show 8 @score = Score.find(params[:id]) 9 end 10 def create 11 @score = Score.new(score_params) 12 13 if @score.save 14 flash[:success] = 'Score が正常に投稿されました' 15 redirect_to @score 16 else 17 flash.now[:danger] = 'Score が投稿されませんでした' 18 render :new 19 end 20 end 21 22 def edit 23 @score = Score.find(params[:id]) 24 end 25 26 def update 27 @score = Score.find(params[:id]) 28 29 if @score.update(score_params) 30 flash[:success] = 'Score は正常に更新されました' 31 redirect_to @score 32 else 33 flash.now[:danger] = 'Score は更新されませんでした' 34 render :edit 35 end 36 end 37 38 def destroy 39 @score = Score.find(params[:id]) 40 @score.destroy 41 42 flash[:success] = 'Score は正常に削除されました' 43 redirect_to scores_url 44 end 45 46 private 47 48 # Strong Parameter 49 def score_params 50 params.require(:score).permit(:hole_score) 51 end 52end 53

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

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

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

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

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

winterboum

2020/09/01 12:35

Topページのviewとそこに行くcontrollerも載せてください
hurousyotoku500

2020/09/01 12:40

追記させて頂きました。宜しくお願い致します。
winterboum

2020/09/01 13:20

おかしな所がみあたらん。。。。
winterboum

2020/09/01 22:27

ブラウザで〇〇/usersにアクセス した時の logを見せてください。 Started GET から次のStartもしくは終わりまで
hurousyotoku500

2020/09/02 03:41

すみません、どのようなツールを使うとログが見れるのでしょうか。
winterboum

2020/09/02 03:49

OSは何でしょう、、、 unix系でしたら less log/development.log でみれます。 Windowsですと、、、 more ってありましたっけ textファイルですのでeditorでも見ることはできます
hurousyotoku500

2020/09/02 03:53

OSはMacですね。 less log/development.logはターミナルに入力ですか?
hurousyotoku500

2020/09/02 12:08

Started GET "/" for 122.222.130.5 at 2020-08-23 05:09:45 +0000 Cannot render console from 122.222.130.5! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Rails::WelcomeController#index as HTML Rendering /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.4.3/lib/r ails/templates/rails/welcome/index.html.erb Rendered /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.4.3/lib/ra ils/templates/rails/welcome/index.html.erb (1.6ms) Completed 200 OK in 4ms (Views: 2.8ms | ActiveRecord: 0.0ms)
hurousyotoku500

2020/09/02 12:10

こちらでよろしいでしょうか。 すみません、原因不明で特にコードはいじっていないのですが、/usersがみれるようになりました。参考までにless log/development.logでどのような情報が得られるのか教えていただけないでしょうか。
winterboum

2020/09/02 12:15

Started GET "/users" になっているかどうか Processing by Rails::UsersController#index as HTML  になっているかどうか Rendered users/index.html.erb になっているかどうか
hurousyotoku500

2020/09/02 12:26

上記全て表示されていました。ありがとうございます。 表示されない場合はここのログを確認する癖をつけます。 ありがとうございました。
guest

回答1

0

自己解決

原因不明だが、表示されるようになった。
表示されない場合はまずログを見ること。

投稿2020/09/02 12:27

hurousyotoku500

総合スコア27

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問