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

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

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

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

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Q&A

解決済

1回答

839閲覧

application.html.erbにデータベースのデータを表示させたい。

kkk_h

総合スコア1

Ruby

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

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

0グッド

0クリップ

投稿2021/09/12 09:21

編集2021/09/12 11:44

前提・実現したいこと

railsでTwitterのようなシステムと体重管理のシステム融合したを作っています。
application.html.erbにweightのデータベースに保存した情報を表示させたい。
データベースに情報を入れることはできました。

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

NoMethodError in Snsposts#index /diet_app/app/views/layouts/application.html.erb where line #66 raised: undefined method `target' for #<Weight::ActiveRecord_Relation:0x00007faa01cccfb0> Extracted source (around line #66): </div> </div> <p class="weight">Target weight : <%= @weights.target %>kg</p> <p class="weight">Current weight : kg</p> <div class="calorie"> <h2>Daily target calorie intake</h2>

該当のソースコード

applicationhtmlerb

1<!DOCTYPE html> 2<html> 3 <head> 4 <title>DietApp</title> 5 <%= csrf_meta_tags %> 6 <%= csp_meta_tag %> 7 <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css"> 8 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 9 <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> 10 </head> 11 12 <body> 13 <div class="main"> 14 <div class="inner"> 15 16 <!-- home_box --> 17 <div class="home_box"> 18 <div class="home_inner"> 19 <h1><a href="/">Dieter</a></h1> 20 <div class="home_link"> 21 <a href="/">Home</a> 22 <% if user_signed_in? %> 23 <%= link_to "Profile", "/users/#{current_user.id}" %> 24 <a href="">Calorie intake</a> 25 <% end %> 26 </div> 27 <div class="home_post"> 28 <% if user_signed_in? %> 29 <%= link_to "Target weight", new_weight_path, class: "post" %> 30 <%= link_to "Record", root_path, class: "post" %> 31 <%= link_to "Post", new_snspost_path, class: "post" %> 32 <%= link_to "Sign out", destroy_user_session_path, method: :delete %> 33 <% else %> 34 <%= link_to "Sign in", new_user_session_path, class: "post" %> 35 <%= link_to "Sign up", new_user_registration_path, class: "post" %> 36 <% end %> 37 </div> 38 <div class="home_profile"> 39 <% if user_signed_in? %> 40 <% if current_user.image.attached? %> 41 <%= image_tag current_user.image, class: 'home_profile_img'%> 42 <% end %> 43 <p><%= current_user.nickname %></p> 44 <% end %> 45 </div> 46 </div> 47 </div> 48 <!-- /home_box --> 49 50 <%= yield %> 51 52 <!-- weight_box --> 53 <div class="weight_box"> 54 <div class="goal_box"> 55 <div class="goal_inner"> 56 <div class="goal_title"> 57 <h1>Target weight</h1> 58 <div class="weight_drop"> 59 <img src="/assets/down.png" alt=""> 60 <div class="weight_drop_box"> 61 <a href="">編集</a> 62 <a href="">削除</a> 63 </div> 64 </div> 65 </div> 66 67       <!------------ここです-------------------- 68 <p class="weight">Target weight : <%= @weights.target %>kg</p> 69 ----------------------------------------> 70 71 <p class="weight">Current weight : kg</p> 72 <div class="calorie"> 73 <h2>Daily target calorie intake</h2> 74 <p>kcal</p> 75 </div> 76 </div> 77 </div> 78 <div class="record_box"> 79 <div class="record_inner"> 80 <h1>Calorie intake</h1> 81 <div class="food_cal"> 82 <div class="date_box"> 83 <h2>Date : 4/12</h2> 84 <div class="record_drop"> 85 <img src="/assets/down.png" alt=""> 86 <div class="record_drop_box"> 87 <a href="">編集</a> 88 <a href="">削除</a> 89 </div> 90 </div> 91 </div> 92 <p>Food : 274kcal (12:20)</p> 93 <p>Food : 274kcal (12:20)</p> 94 <p>Food : 274kcal (12:20)</p> 95 <p>Food : 274kcal (12:20)</p> 96 <p>Food : 274kcal (12:20)</p> 97 <p>Food : 274kcal (12:20)</p> 98 <p>Food : 274kcal (12:20)</p> 99 <p>Food : 274kcal (12:20)</p> 100 <div class="total_cal"> 101 <h2>Total</h2> 102 <p>2845kcal</p> 103 </div> 104 </div> 105 </div> 106 </div> 107 </div> 108 <!-- /weight_box --> 109 110 </div> 111</div> 112 113 </body> 114</html> 115

その他ソースコード

snspostscontrollerrb

1class SnspostsController < ApplicationController 2 before_action :set_snspost, only: [:edit, :show] 3 before_action :move_to_index, except: [:index, :show] 4 5 def index 6 @snsposts = Snspost.includes(:user).order("created_at DESC") 7 @weights = Weight.all 8 end 9 10 def new 11 @snspost = Snspost.new 12 end 13 14 def create 15 Snspost.create(snspost_params) 16 end 17 18 def destroy 19 snspost = Snspost.find(params[:id]) 20 snspost.destroy 21 end 22 23 def edit 24 end 25 26 def update 27 snspost = Snspost.find(params[:id]) 28 snspost.update(snspost_params) 29 end 30 31 def show 32 @comment = Comment.new 33 @comments = @snspost.comments.includes(:user) 34 end 35 36 private 37 def snspost_params 38 params.require(:snspost).permit(:image, :text).merge(user_id: current_user.id) 39 end 40 41 def set_snspost 42 @snspost = Snspost.find(params[:id]) 43 end 44 45 def move_to_index 46 unless user_signed_in? 47 redirect_to action: :index 48 end 49 end 50 51end 52

weightsControllerrb

1class WeightsController < ApplicationController 2 3 def new 4 @weight = Weight.new 5 end 6 7 def create 8 Weight.create(weight_params) 9 redirect_to root_path 10 end 11 12 private 13 def weight_params 14 params.require(:weight).permit(:target, :current, :intake) 15 end 16end 17

snspostrb

1class CreateSnsposts < ActiveRecord::Migration[6.0] 2 def change 3 create_table :snsposts do |t| 4 t.string :text 5 t.integer :user_id, null: false 6 t.timestamps 7 end 8 end 9end 10

weightsrb

1class CreateWeights < ActiveRecord::Migration[6.0] 2 def change 3 create_table :weights do |t| 4 t.integer :target 5 t.integer :current 6 t.integer :intake 7 t.timestamps 8 end 9 end 10end 11

snspostrb

1class Snspost < ApplicationRecord 2 validates :text, presence: true 3 belongs_to :user 4 has_many :comments 5 has_one_attached :image 6end

weightrb

1class Weight < ApplicationRecord 2end

routesrb

1Rails.application.routes.draw do 2 devise_for :users, controllers: { registrations: 'users/registrations' } 3 root to: "snsposts#index" 4 resources :snsposts do 5 resources :comments, only: :create 6 end 7 resources :users, only: [:show, :edit, :update] 8 resources :comments, only: :destroy 9 resources :weights, only: [:new, :create] 10end 11

試したこと

解決方法を自分なりに検索したが、うまくヒットせず止まってしまいました。
誤字がないかの確認はしました。

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

Rails 6.0.4.1
ruby 2.6.5
mysql Ver 14.14 Distrib 5.6.51, for osx10.16 (x86_64) using EditLine wrapper

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

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

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

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

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

m.ts10806

2021/09/12 10:56

>(プログラミング初学者で、説明不足な部分や情報の公開不足な部分、初歩的なミスが多いです。不快な質問をしてしまっている可能性があります。申し訳ございません。説明不足な部分や情報の公開不足な部分がありましたら、ご指摘してください。) 書かなくて良いです。 質問に「初心者アイコン」をつければ伝わります。 あと一応確認したいのですが、Railsチュートリアルは通されましたよね?
kkk_h

2021/09/12 11:00

ご指摘ありがとうございます。 ~質問に「初心者アイコン」をつければ伝わります。~ 大変有益な情報ありがとうございます。今後活用します。 ~あと一応確認したいのですが、Railsチュートリアルは通されましたよね?~ 一通り通しました。
m.ts10806

2021/09/12 11:40

質問は編集できますので、初心者アイコンは忘れないうちに今からでも。
m.ts10806

2021/09/12 11:41

>一通り通しました。 ではもう1回通した方が良いですね。 テンプレートファイルにもちゃんと役割があります。
kkk_h

2021/09/12 11:46

ご指摘ありがとうございます。 初心者アイコンをつけました。 もう一度、Railsチュートリアルに戻り学習します。
guest

回答1

0

ベストアンサー

layouts/application.html.erb に、ですか、、、、、
これはすべての画面で使われますので、すべての場合に @weights を設定しないとエラーで止まります。それで良い?

というのがありますが、
今回のエラーは、
モデル Weight に項目 target はありますが、
@weights = Weight.all
これは Weiget のインスタンスではなく、その集合なので、targetはありません。

全部を表示させるのか最後のだけ載せるのか、ですね。
最後のだけなら @weights.last.target

投稿2021/09/12 09:46

winterboum

総合スコア23416

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問