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

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

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

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

Ruby

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

Ruby on Rails

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

セッション

Sessionはクライアントがサーバに送ったすべてのリクエストのことを指します。

データベース

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

Q&A

解決済

1回答

3869閲覧

Ruby on Rails5 ECサイトカート機能の実装 DBのテーブルに値が入らない

tomato185

総合スコア29

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

セッション

Sessionはクライアントがサーバに送ったすべてのリクエストのことを指します。

データベース

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

0グッド

1クリップ

投稿2020/09/06 10:55

編集2020/09/06 22:48

前提・実現したいこと

Ruby on rails 初心者です。
現在、ECサイトの構築を行っており、つまずいたのはセッション機能を用いた「カートにいれる」処理です。
参考にしているサイト「https://qiita.com/kenzo-ta/items/b45994c5f3fdd87b6c50」

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

エラーメッセージはでず、「carts_controller」の「create」アクションで@cart_item.saveされず、elseの方に回ってしまいます。 show.html.erbの「カートに追加」ボタンを押下すると、「application_controller」「carts_controller」を経由して、 cartsテーブル(user_idカラム)/cart_itemsテーブル(quantity/product_id/cart_idカラム)にデータが入る仕組み なのですが、両テーブルともにデータが入りません。

該当のソースコード

(app/vies/products/show.html.erb) <div class="cart"> <%= form_for(:session, url: carts_path) do |f|%> <%= f.label :quantity, "数量"%> <%= f.select :quantity, [1,2,3,4,5,6,7,8,9,10]%> <%= f.submit "カートに追加", class:"btn cart_btn" %> <% end %> </div>
(app/controllers/carts_controller) class CartsController < ApplicationController before_action :set_line_item, only: [:create, :destroy] before_action :set_user before_action :set_cart def create @cart_item = @cart.cart_items.build(product_id: params[:product_id]) if @cart_item.blank? if @cart_item.save redirect_to current_cart else redirect_to products_path end end def destroy @cart.destroy redirect_to current_cart end private def set_user @user = current_user end def set_line_item @cart_item = current_cart.cart_items.find_by(product_id: params[:product_id]) end def set_cart @cart = current_cart end end
(app/controllers/application_controller)一部記載 def current_cart if session[:cart_id] current_cart = Cart.find_by(id: session[:cart_id]) session[:cart_id] = current_cart.id current_cart else current_cart = Cart.create(user_id: current_user.id) session[:cart_id] = current_cart.id current_cart = Cart.find_by(id: session[:cart_id]) current_cart end end
(app/models/cart_item.rb) class CartItem < ApplicationRecord belongs_to :product belongs_to :cart belongs_to :user end
(app/models/cart.rb) class Cart < ApplicationRecord has_many :cart_items end
(app/models/user.rb)一部記載 has_many :carts

試したこと

アソシエーション系の問題ではないかと思い、has_manyやbelong_toを再確認した。

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

ruby 2.6.6
rails '~> 5.2.4', '>= 5.2.4.3'

編集後/最新コード

(app/vies/products/show.html.erb) <div class="cart"> <%= form_for(:session, url: cart_items_path) do |f|%> <%= f.label :quantity, "数量"%> <%= f.select :quantity, [1,2,3,4,5,6,7,8,9,10]%> <%= f.hidden_field :product_id, value: @product_info.id %> ←★変更点 <%= f.submit "カートに追加", class:"btn cart_btn" %>   <% end %> </div>
(app/controllers/cart_items_controller)←★変更点(carts_controller.rbの内容を全てcart_items_controller.rb)に移動 class CartItemsController < ApplicationController before_action :set_line_item, only: [:create, :destroy] before_action :set_user before_action :set_cart def create @cart_item = @cart.cart_items.build(product_id: session[:product_id]) if @cart_item.blank? ←★変更点(session[:product_id]) if @cart_item.save! ←★変更点 redirect_to current_cart else redirect_to products_path end end def destroy @cart.destroy redirect_to current_cart end private def set_user @user = current_user end def set_line_item @cart_item = current_cart.cart_items.find_by(product_id: params[:product_id]) end def set_cart @cart = current_cart end end
(app/controllers/application_controller)一部記載 def current_cart if session[:cart_id] current_cart = Cart.find_by(id: session[:cart_id]) session[:cart_id] = current_cart.id current_cart else current_cart = Cart.create(user_id: current_user.id) session[:cart_id] = current_cart.id current_cart = Cart.find_by(id: session[:cart_id]) current_cart end end
(app/models/cart_item.rb) class CartItem < ApplicationRecord belongs_to :product belongs_to :cart belongs_to :user end
(app/models/cart.rb) class Cart < ApplicationRecord has_many :cart_items belongs_to :user ←★変更点 end
(app/models/user.rb)一部記載 has_many :carts
(app/controllers/products_controller.rb)一部記載 def show @product_info = Product.find_by(id: params[:id]) @product_images = @product_info.images end

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

以下のようなエラーとなりました。 cartテーブルにはデータ(current_user.id=user_id)が入りました。 ただ、cart_itemsのテーブルにはデータが登録されませんでした。 Validation failed: Product must exist Extracted source (around line #9): 7  def create 8   @cart_item = @cart.cart_items.build(product_id: session[:product_id]) if @cart_item.blank? 9 if @cart_item.save! 10   redirect_to current_cart 11 else 12  redirect_to products_path

if @cart_item.save!の「!」を外すと、elseに回って、products_path(商品一覧画面)にいきます。

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

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

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

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

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

guest

回答1

0

ベストアンサー

色々おかしいです。

  1. current_cart = Cart.create(user_id: current_user.id) なのにclass Cart に bilongs_to :user がありません。
  2. cart_controller の create で cart でなく cart_itemを作ってます
  3. cart_items.build(product_id: params[:product_id]) とありながら form_for(:session, url: carts_path) には product_id を渡す記述がありません。

2はエラーの直接的原因ではないですが、混乱の元ですので cart_items_controller にやらせましょう。

エラーの直接の原因は 3 です。
が、関連がおかしいのも遠因になってます。
関連を直し、controllerを変えて、viewを直しましょう。
products/show.html なのですから、productは分かってますよね?

うまくいかなかった場合は、
最新のcodeを載せ、一時的に @cart_item.save! と ! をつけて実行し画面に出たエラーを載せてください

追記

原因は明白、@cart.cart_items.build で product_id が渡されていないから。
はてな? が2つ。

  1. session[:product_id] ってどこで定義してます?
  2. f.hidden_field :product_id, value: @product_info.id とありますが、@product_info ?

products の show なので productが渡ってると思ったのですが違う?
show action載せてください。

で、ですね。
欲しい値はsession[:product_id]とか@product_info.id とか書けば万能の召喚呪文の如くそこに現れるわけではないですよ。召喚する前に仕込んで置かなければ。

追記2

Product のインスタンスは @product がよいです。@product_info とするとProductInfo という別のモデルのインスタンスであると思われます。
@product_info は @product になおした、として
そうしますと f.hidden_field :product_id, value: @product.id により params[:session][:product_id] ができているのでそれを使いましょう。
「該当商品のidをsession[:product_id]に入れました。」とありますが入れていないので。

ただ、
form_for(:session, url: cart_items_path) のsession がいかにも不自然です。
form_for(@product, url: cart_items_path)にして、params[:product][:product_id] が自然です。
@cart.cart_items.build(product_id: params[:product][:product_id]) ですね。
なお if @cart_item.blank? は不要です。actionに入った直後なので必ず blankです。

###蛇足
sessionを使う必要があるのか? paramsでcart_idを渡せば済む。
そもそもuserが has_many :carts な必要があるのか? has_one :cart で十分では?
それならparamsで渡す必要もない。 @cart = current_user.cart || Cart.create(user_id: current_user.id)とか@cart = cCart.find_or_create_by(user_id: current_user.id)で済む

投稿2020/09/06 11:57

編集2020/09/06 23:23
winterboum

総合スコア23331

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

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

tomato185

2020/09/06 13:09

winterboum様 ご回答、ありがとうございます。 早速、3点の修正をいたしました。 エラー文/最新版コードは上記の通りです。
tomato185

2020/09/06 22:51

winterboum様 ご回答、ありがとうございます。 showアクションを追記しました。 showアクションで@product_infoを定義し、 そこからf.hidden_field :product_id, value: @product_info.idで該当商品のidをsession[:product_id]に入れました。
tomato185

2020/09/06 23:14

あ、、、 申し訳ありません。 「召喚する前に仕込んで置かなければ。」で気づきました。 showアクションにsession[:product_id]を仕込んでいませんでした。。。
winterboum

2020/09/06 23:21

おお、魔法の呪文にかかってくれた
tomato185

2020/09/06 23:34

winterboum様 ご回答ありがとうございます!! 実際に、動作した後、ふと思ったのですが、session使う必要があるか??と思い始めました。 javaでECサイトを作成した時はsessionを使っていたので、今回も同様にしようと思っていました。。。 さて、一つ疑問なのですが、 ・そもそもuserが has_many :carts な必要があるのか? についてなのですが、ユーザーがいくつも商品をカゴにいれることを考えると、has_many の方が良いのでは?と考えたのですが、そのケースでもhas_oneでいけますか??
tomato185

2020/09/06 23:46

winterboum様 大変申し訳ありません。。。 追記2を試してみたのですが、 急に、<%= f.select :quantity, [1,2,3,4,5,6,7,8,9,10]%>でquantityが定義されていないよ。と エラーが出たのですが、何が原因なのでしょうか。
winterboum

2020/09/07 00:15

そか、Productを渡したのは失敗でした。 form_for(@product  を form_for( CartItem.new にしてください。 CartItem は quantity持ってますよね? あれ、、、 @cart.cart_items.build(product_id: は product_id しかいれてないですね。 個数は入れなくて良いの? formというのは、そこに書いてあれば自動で然るべきところに収まるという魔法の呪文ではないですよ
tomato185

2020/09/07 00:22

winterboum様 ご回答、ありがとうございます。 <%= form_for(CartItem.new, url: cart_items_path) do |f|%>に変更しました。 申し訳ありません、加えております! @cart_item = @cart.cart_items.build(product_id: params[:product][:product_id], quantity: params[:product][:quantity]) ↑のように変更した場合、params[:product][:product_id]の部分はどのように変更したらよろしいでしょうか。
winterboum

2020/09/07 00:32

params[:product] の部分をparams[:cart_item] かな。 logをみるとparamsがわかるので確認してくdさい
tomato185

2020/09/07 00:51

winterboum様 ご回答、ありがとうございます! 無事にデータが入りました!!感動です!! ありがとうございます!!!! もしよければ、今後もwinterboum様にruby on railsについて質問させていただきたいのですが、 よろしいでしょうか??
winterboum

2020/09/07 01:29

どうぞよろこんで
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問