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

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

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

Haml(HTML abstraction markup language)は、HTML/XHTMLを効率的に記述するためのマークアップ言語および記法です。

Ruby on Rails

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

Q&A

解決済

1回答

1282閲覧

uninitialized constant を解決したい

harabou

総合スコア5

Haml

Haml(HTML abstraction markup language)は、HTML/XHTMLを効率的に記述するためのマークアップ言語および記法です。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/03/01 03:24

前提・実現したいこと

railsでフリマアプリを作っています
マイページの、出品中リストを表示させる機能を実装中に以下のエラーメッセージが発生しました。

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

NameError in Users#exhibitionList uninitialized constant User::SellingItem

該当のソースコード

haml

1=render 'shared/header' 2.form-page 3 .my-page 4 =render 'shared/leftBar' 5 .my-status 6 .my-status__purchased-items 7 .purchased-title 8 出品した商品 9 .purchased-tabs 10 = link_to '#', id: 'during-trading-tab', class: 'mypage-tab' do 11 出品中 12 = link_to '#', id: 'transacted-tab', class: 'mypage-tab' do 13 売却済み 14 15 .purchased-lists 16 - if @user.selling_items.present? 17 - @items.each do |item| 18 %ul 19 %li 20 =link_to "#" do 21 .image-box 22 = image_tag item.images[0].image.url 23 .text 24 = item.title[0] 25 .icon-arrow-right 26 = icon('fas', 'chevron-right') 27 - else 28 .purchased-lists--message 29 出品中の商品がありません 30=render 'shared/footer'

model

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 validates :nickname, presence: true 8 validates :password, length: { minimum: 7 } 9 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 10 validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX } 11 VALID_BIRTHDAY_REGEX = /\A[0-9]+\z/ 12 validates :birthday, presence: true, length: { is: 8 }, format: { with: VALID_BIRTHDAY_REGEX } 13 VALID_NAME_REGEX = /\A[ぁ-んァ-ン一-龥]/ 14 validates :lastname, presence: true, format: { with: VALID_NAME_REGEX } 15 validates :firstname, presence: true, format: { with: VALID_NAME_REGEX } 16 VALID_NAME_KANA_REGEX = /\A[ァ-ヶー-]+\z/ 17 validates :lastname_kana, presence: true, format: { with: VALID_NAME_KANA_REGEX } 18 validates :firstname_kana, presence: true, format: { with: VALID_NAME_KANA_REGEX } 19 20 has_many :cards 21 belongs_to :address 22 has_many :selling_items, -> { where("user_id is not NULL && bought_user_id is NULL") } 23end

contriller

1class UsersController < ApplicationController 2 3 def show 4 @address = Address.find_by(user_id: current_user.id) 5 end 6 7 def edit 8 end 9 10 def update 11 if current_user.update(user_params) 12 redirect_to root_path 13 else 14 render :edit 15 end 16 end 17 18 def info 19 end 20 21 def purchaseList 22 end 23 24 def exhibitionList 25 @items = Item.where(user_id: current_user.id) 26 @user = User.find(current_user.id) 27 end 28 29 def soldList 30 end 31 32 def contact 33 end 34 35 private 36 37 def user_params 38 params.require(:user).permit(:nickname, :image, :email, :password, :password_confirmation, :lastname, :firstname, :lastname_kana, :firstname_kana, :birthday) 39 end 40end 41

ご回答宜しくお願い致します。

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

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

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

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

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

siruku6

2020/03/01 06:01

error内容はほかにも表示されていると思いますが、いかがでしょうか? エラーが発生したファイル名や行数が記載されているはずなので、それらも全て質問に掲載した方が回答が付きやすくなると思います。 また、SellingItemという文字列が掲載されているコードには見当たらないのですが、どこかに記載してはいないのでしょうか?
shinoharat

2020/03/03 06:54

siruku6 さんと少し内容が被ってますが、私からも質問です。 【質問1】 User モデルに has_many :selling_items, -> { where("user_id is not NULL && bought_user_id is NULL") } のような定義がありますが、 SellingItem というモデルは存在しますか?しませんか? 【質問2】 ひょっとして、 has_many :selling_items, .... の部分でやりたいことは 「(SellingItemではなく)Itemモデルに対する紐づけ」 だったりします?
guest

回答1

0

自己解決

2名の方からご質問いただいたのに、返信せずに申し訳ございませんでした。
モデルを作らずに表示する方法を取りました。
やりたいことと、記述したコードがそもそもあっていなかったようです。

controllerで取り出したい情報を指定
@selling_itemsはuser_idがログイン中のユーザーでbought_user_idを持っていないアイテム

controller

1class UsersController < ApplicationController 2 3 def purchaseList 4 @bought_items = Item.where(bought_user_id: current_user) 5 end 6 7 def exhibitionList 8 @selling_items = Item.where(user_id: current_user).where(bought_user_id: nil) 9 end 10 11 def soldList 12 @sold_items = Item.where(user_id: current_user).where.not(bought_user_id: nil) 13 end 14

@selling_itemsがなければ"出品中の商品がありません"
あればeach文で表示できるようにする

haml

1- if @selling_items.present 2 - @selling_items.each do |item| 3 %ul 4 %li 5 =link_to item_path(item) do 6 .image-box 7 = image_tag item.images[0].image.url 8 .text 9 = item.title 10 .icon-arrow-right 11 = icon('fas', 'chevron-right') 12- else 13 .purchased-lists--message 14 出品中の商品がありません

質問の仕方を、きちんと考えて投稿しないといけないなと反省致しました。

投稿2020/03/04 07:29

harabou

総合スコア5

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問