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

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

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

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

MySQL

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

Ruby on Rails

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

Q&A

解決済

1回答

765閲覧

ruby データをuserと紐づけて、紐づいたuserの値のみを出力させたい

branchpeach

総合スコア12

Ruby

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

MySQL

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/05 10:32

編集2021/04/05 10:41

前提・実現したいこと

データをuserと紐づけて、紐づいたuserの値のみを出力させたい
イメージ説明

以上の画像のように収支をグラフと該当月の収入と支出という形で表示させているのですが、これがユーザーと紐づいておらず、全てのユーザーの合計値になっています。

これをユーザーごとに違う表示にさせたいです。

ちなみに、こちらの一覧表示は<% if current_user.id == spending.user.id %>と記述をすることで、userとspending(支出)のuserが一致するときのみ表示するようにしています。
イメージ説明

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

イメージ説明

該当のソースコード

ruby

1income.html.erb(全体) 2 3<div class="footerFixed"> 4 <%= render "shared/header" %> 5 <div class="post-content-parent"> 6 <div class="post-content"> 7 <div class="left-content"> 8 <div class="title"><%= @this_month %>月の支出</div> 9 <div class="post-list-content"> 10 <% @spendings_time.each do |spending| %> 11 <% if current_user.id == spending.user.id %> 12 <ul class="post-list"> 13 <li class="post-list-data-date"><%= spending.date %></li> 14 <ul> 15 <li><%= link_to '詳細', spending_path(spending.id), class:"link_pass" %></li> 16 <li><%= link_to '編集', edit_spending_path(spending.id), class:"link_pass" %></li> 17 <li><%= link_to '削除', spending_path(spending.id), class:"link_pass", method: :delete %></li> 18 </ul> 19 <li class="post-list-data"><%= spending.category %></li> 20 <li class="post-list-data"><%= image_tag 'money.png', size: '50x50' %>¥<%= spending.price %></li> 21 </ul> 22 <% end %> 23 <% end %> 24 </div> 25 </div> 26 <div class="center-content"> 27 <div class="title"><%= @this_month %>月の収入</div> 28 <div class="post-list-content"> 29 <% @incomes_time.each do |income| %> 30 <% if current_user.id == income.user.id %> 31 <ul class="post-list"> 32 <li class="post-list-data-date"><%= income.date %></li> 33 <ul> 34 <li><%= link_to '詳細', income_path(income.id), class:"link_pass" %></li> 35 <li><%= link_to '編集', edit_income_path(income.id), class:"link_pass" %></li> 36 <li><%= link_to '削除', income_path(income.id), class:"link_pass", method: :delete %></li> 37 </ul> 38 <li class="post-list-data"><%= income.category %></li> 39 <li class="post-list-data"><%= image_tag 'money.png', size: '50x50' %>¥<%= income.price %></li> 40 </ul> 41 <% end %> 42 <% end %> 43 </div> 44 </div> 45 <div class="right-content"> 46 <div class="title"><%= @this_month %>月の収支</div> 47 <% if user_signed_in? && current_user.id == @spending.user.id %> 48 49 <div class="right-content-post"> 50 <div class="right-content-upper"> 51 <%= pie_chart @spending_data.group(:category).sum(:price), thousands: ",", width: "450px", height:"450px", class:"date-graph", library: {title: {text: "今月の支出 計 #{@spending_sum}円"}} %> 52 </div> 53 <div class="right-content-bottom"> 54 <div class="income-sum"> 55 今月の収入 <%= @income_sum %>円 56 </div> 57 <div class = "spending-sum"> 58 今月の出費 -<%= @spending_sum %>円 59 </div> 60 <div class="expense-sum"> 61 収支計 <%= @expense_sum %>円 62 </div> 63 </div> 64 </div> 65 <% end %> 66 </div> 67 </div> 68 <%= render "shared/footer" %> 69</div> 70 71

ruby

1income.html.erb(該当の場所) 2 3<div class="right-content"> 4 <div class="title"><%= @this_month %>月の収支</div> 5 <% if user_signed_in? && current_user.id == @spendings.user.id %> 6 7 <div class="right-content-post"> 8 <div class="right-content-upper"> 9 <%= pie_chart @spending_data.group(:category).sum(:price), thousands: ",", width: "450px", height:"450px", class:"date-graph", library: {title: {text: "今月の支出 計 #{@spending_sum}円"}} %> 10 </div> 11 <div class="right-content-bottom"> 12 <div class="income-sum"> 13 今月の収入 <%= @income_sum %>円 14 </div> 15 <div class = "spending-sum"> 16 今月の出費 -<%= @spending_sum %>円 17 </div> 18 <div class="expense-sum"> 19 収支計 <%= @expense_sum %>円 20 </div> 21 </div> 22 </div> 23 <% end %> 24 </div>

ruby

1 2incomesコントローラー 3 4class IncomesController < ApplicationController 5 before_action :authenticate_user! 6 before_action :income_id, only: [:show, :edit, :update, :destroy] 7 before_action :income_user, only: [:index, :search] 8 before_action :spending_user, only: [:index,:search, :show] 9 before_action :move_to_index, only: [:show, :edit, :destroy, :update, :create] 10 require "time" 11 12 def index 13 @spendings_time = Spending.where(date: Time.now.beginning_of_month..Time.now.end_of_month).includes(:user).order(date: "ASC") 14 @incomes_time = Income.where(date: Time.now.beginning_of_month..Time.now.end_of_month).includes(:user).order(date: "ASC") 15 @spending_sum = @spendings_time.sum(:price) 16 @income_sum = @incomes_time.sum(:price) 17 @expense_sum = @income_sum - @spending_sum 18 @spending_data = Spending.where(date: Time.now.beginning_of_month..Time.now.end_of_month).includes(:user) 19 @this_month = Time.new.month 20 end 21 22 def new 23 @income = Income.new 24 end 25 26 def create 27 @income = Income.new(income_params) 28 if @income.save 29 redirect_to root_path 30 else 31 render :new 32 end 33 end 34 35 def show 36 end 37 38 def edit 39 end 40 41 def update 42 if @income.update(income_params) 43 redirect_to root_path 44 else 45 render :edit 46 end 47 end 48 49 def destroy 50 @income.destroy 51 redirect_to root_path 52 end 53 54 def search 55 end 56 57 private 58 def income_params 59 params.require(:income).permit(:price, :category, :memo, :date).merge(user_id: current_user.id) 60 end 61 def income_id 62 @income = Income.find(params[:id]) 63 end 64 def income_user 65 @incomes = Income.includes(:user).order(date: "ASC") 66 end 67 def spending_user 68 @spendings = Spending.includes(:user).order(date: "ASC") 69 end 70 def move_to_index 71 if current_user.id != @income.user.id 72 redirect_to root_path 73 end 74 end 75end 76

試したこと

  • incomesコントローラーの以下の記述で@spendingsを定義し、
def spending_user @spendings = Spending.includes(:user).order(date: "ASC") end

index.htnl.erbファイルで以下のようにし、userと紐づいているものだけ表示させようとしたが、上で表示させているエラー文がでた。
<% if user_signed_in? && current_user.id == @spendings.user.id %>

  • incomesコントローラー内で

spending = Spending.find(params[:id])を定義したが、idがないエラーが出る。
→コントローラーが違うのでもちろんspendingのidはない。

  • 以下の記述でuserを紐付け
def spending_user @spendings = Spending.includes(:user).order(date: "ASC") end

index.html.erbファイルにと記述するときになぜuserがエラーが出るのか。
findメソッドを使えば解決しそうだがspending = Spending.find(params[:id])の書き方だとエラーが出る。
<% if user_signed_in? && current_user.id == @spendings.user.id %>

どなたか初心者ですが、ご教授いただけると幸いです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

@spendings は Spending の インスタンスではなく、その集合体です。
ので、 user はありません。
Spending の中で current_user のものだけ抜き出したいということですから
@spendings = Spending.where(user_id: current_user.id).includes(:user).order(date: "ASC")としてください。

投稿2021/04/05 11:33

編集2021/04/05 11:34
winterboum

総合スコア23344

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

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

branchpeach

2021/04/05 11:47

`@spendings = Spending.where(user_id: current_user.id).includes(:user).order(date: "ASC")` と記述すると上記のエラー文と同じようにuserがno method errorになります。 `<% if user_signed_in? && current_user.id == @spendings.user.id %> ` この記述方法が間違っていますでしょうか?
branchpeach

2021/04/05 12:18

スペルミスでした。できました。二箇所もご回答ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問