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

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

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

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

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

Q&A

0回答

608閲覧

NoMethodErrorのエラーが出て先に進めません

yukitodesu3

総合スコア10

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/04/15 01:15

railsでwebアプリケーションを作っています。アプリケーション作成中NomethodErrorのエラーが出て、先に進むことができません。itemモデルのデータを取り出そうと思い、コードをかいたところエラーが発生しました。itemモデルの他のカラム(nameなど)は表示できていたので、なぜpriceカラムだけが表示できないのかわかりません。コードを自分なりに見直して見たのですが原因がわかりません。お忙しいとは思いますが、回答よろしくお願いします。

イメージ説明

仕様
・itemモデルとcart_itemモデルは1対Nの関係になっています。
・deviseを使っています。

item.rb

class Item < ApplicationRecord attachment :image belongs_to :genre has_many :cart_items, dependent: :destroy end

cart_item.rb

class CartItem < ApplicationRecord belongs_to :item belongs_to :customer end

show.html.rrb

<div class="container"> <div class="row"> <div class="col"> <div class="product-show-title"> <div class="top-item"><%= attachment_image_tag @item,:image, fallback: "no_image.jpg", size:'200x200' %></div> <p class=""><%= @item.name %></p> <p class=""><%= @item.introduction %></p> <p class=""><span>¥<%= @item.price %></span>(税込)</p> </div> <div class="product-show-list"> <%= form_for (@cart_item),url: public_cart_items_path do |f| %> <%= f.number_field(:amount, in: 1..100) %> <%= f.hidden_field :item_id, :value => @item.id %> <%= f.hidden_field :customer_id, :value => current_customer.id %> <%= f.submit "カートに入れる" %> <% end %> </div> </div> </div> </div>

index.html.erb

<div class="container"> <div class="row"> <div class="col"> <div class="cart_item-list"> <div class="cart-item-title"> <h4>ショッピングカート</h4> </div> <table class="table"> <thead> <tr> <th>商品名</th> <th>単価(税込)</th> <th>数量</th> <th>小計</th> <th></th> </tr> </thead> <tbody> <tr> <% @cart_items.each do |cart_item| %> <td> <%= cart_item.item.image %> <%= cart_item.item.name %> </td> <td><%= cart_item.item.price %></td> <td><%= cart_item.amount %></td> <% end %> <% @total.each do |cart_item| %> <td><%= cart_item.total %></td> <% end %> </tr> </tbody> </table> </div> </div> </div> </div>

routes.rb

Rails.application.routes.draw do devise_for :customers devise_for :admins namespace :public do get 'sign_in' => 'sessions#new' get 'sign_up' => 'registrations#new' resources :items,only:[:index,:show,:create] resources :homes,only:[:index] resources :cart_items,only:[:show,:create,:update,:index] end namespace :admin do get 'sign_in' => 'sessions#new' resources :items, only: [:create, :index, :show, :edit, :update, :destroy,:new] resources :genres, only: [:index, :create,:edit, :update] resources :customers, only: [:index, :create, :new, :update, :destroy, :show] end root 'admin/items#top' get 'about' =>'admin/items#about' end

schema.rb

create_table "cart_items", force: :cascade do |t| t.integer "item_id" t.integer "customer_id" t.integer "amount" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "items", force: :cascade do |t| t.integer "genre_id" t.string "name" t.string "image_id" t.text "introduction" t.integer "price" t.boolean "is_active" t.datetime "created_at", null: false t.datetime "updated_at", null: false end

試したこと
・コードの確認
・エラー文の確認

よろしくお願いします

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問