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

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

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

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

Ruby

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

Ruby on Rails

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

Q&A

解決済

1回答

483閲覧

Railsでカートに商品を追加しようとすると、ActionView::MissingTemplate in Customers::Carts#my_cartというエラーが出ました。

nijima

総合スコア27

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/09/01 11:58

Rails学習中のプログラミング初心者です。

現在、ECサイトを作成しております。その中で、カートに商品を追加しようとすると、図1のようなエラーが生じました。
ディレクトリーは図2のようになっており、正しく記入できていると思うのですが、なぜエラーが起きているのでしょうか。
cartsコントローラーをnamepleceを用いて、Customersの配下に置いたため、パーシャルの記入が変わってくるのでしょうか。
大変申し訳ありませんが、エラーの解決方法をご指導お願いいたします。

参考サイト
https://zenn.dev/akhmgc/articles/2d060378c4260e)

(図1)
イメージ説明

(図2)
イメージ説明

(application_contoroller.rb) class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protect_from_forgery with: :exception helper_method :current_cart def current_cart if current_customer # ユーザーとカートの紐付け current_cart = current_customer.cart || current_customer.create_cart! else # セッションとカートの紐付け current_cart = Cart.find_by(id: session[:cart_id]) || Cart.create session[:cart_id] ||= current_cart.id end current_cart end protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) end end
(carts_controller.rb) class Customers::CartsController < ApplicationController before_action :setup_cart_item!, only: %i[add_item update_item delete_item] def my_cart @cart_items = current_cart.cart_items.includes([:item]) @total = @cart_items.inject(0) { |sum, item| sum + item.sum_of_price } end # アイテムの追加 def add_item @cart_item ||= current_cart.cart_items.build(item_id: params[:item_id]) @cart_item.quantity += params[:quantity].to_i if @cart_item.save flash[:notice] = '商品が追加されました。' redirect_to my_cart_path else flash[:alert] = '商品の追加に失敗しました。' redirect_to item_url(params[:item_id]) end end # アイテムの更新 def update_item if @cart_item.update(quantity: params[:quantity].to_i) flash[:notice] = 'カート内のギフトが更新されました' else flash[:alert] = 'カート内のギフトの更新に失敗しました' end redirect_to my_cart_path end # アイテムの削除 def delete_item if @cart_item.destroy flash[:notice] = 'カート内のギフトが削除されました' else flash[:alert] = '削除に失敗しました' end redirect_to my_cart_path end private def setup_cart_item! @cart_item = current_cart.cart_items.find_by(item_id: params[:item_id]) end end
(cart.rb) class Cart < ApplicationRecord has_many :cart_items, dependent: :destroy end
(cart_item.rb) class CartItem < ApplicationRecord belongs_to :item belongs_to :cart def sum_of_price item.price * quantity end end
(routes.rb) Rails.application.routes.draw do devise_for :customers devise_for :admins namespace :customers do resources :items, only: [:index, :show] get '/my_cart' => 'carts#my_cart' post '/add_item' => 'carts#add_item' post '/update_item' => 'carts#update_item' delete '/delete_item' => 'carts#delete_item' end resources :customers namespace :admins do resources :items, only: [:index, :new, :show, :destroy] end put "/customers/:id/hide" => "customers#hide", as: 'customers_hide' root 'homes#top' end
(scheme.rb) ActiveRecord::Schema.define(version: 2021_09_01_113959) do create_table "admins", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "is_deleted", default: false, null: false t.index ["email"], name: "index_admins_on_email", unique: true t.index ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true end create_table "cart_items", force: :cascade do |t| t.integer "quantity", default: 0 t.integer "item_id", null: false t.integer "cart_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["cart_id"], name: "index_cart_items_on_cart_id" t.index ["item_id"], name: "index_cart_items_on_item_id" end create_table "carts", force: :cascade do |t| t.integer "carts_id" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "customer_id" t.index ["carts_id"], name: "index_carts_on_carts_id" t.index ["user_id"], name: "index_carts_on_user_id" end create_table "customers", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.boolean "is_deleted", default: false, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "{:foreign_key=>true}_id" t.index ["email"], name: "index_customers_on_email", unique: true t.index ["reset_password_token"], name: "index_customers_on_reset_password_token", unique: true t.index ["{:foreign_key=>true}_id"], name: "index_customers_on_{:foreign_key=>true}_id" end create_table "items", force: :cascade do |t| t.text "name" t.integer "price" t.integer "stock" t.text "content" t.string "image_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id" end create_table "order_items", force: :cascade do |t| t.integer "item_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["item_id"], name: "index_order_items_on_item_id" end create_table "orders", force: :cascade do |t| t.integer "quantity" t.integer "amount" t.integer "item_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["item_id"], name: "index_orders_on_item_id" end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "is_deleted", default: false, null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーには carts/_cart が無いと書かれています。
画像を見る限り carts/_cart_item (見切れてるけど _cart.html.erb ではない)しか無いですよね。

投稿2021/09/01 14:49

mather

総合スコア6753

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

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

nijima

2021/09/02 03:06

ご指導ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問