フリマアプリをつくっています。
商品詳細ページに、「購入ボタン」を設置し、そのボタンを押すことで、
データベースにbuyer_idが入力され、「購入手続きページ」のようなところに画面遷移させたいです。
ですが、やり方がわかりません。
コードは下記のようになっています。
#Routes
ruby
1class Item < ApplicationRecord 2 3 belongs_to :saler, class_name: "User" 4 belongs_to :buyer, class_name: "User", optional: true 5 6end
ruby
1class User < ApplicationRecord 2 3 has_many :buyed_items, foreign_key: "buyer_id", class_name: "Item" 4 has_many :saling_items, -> {where("buyer_id is NULL") }, foreign_key: "saler_id", class_name: "Item" 5 has_many :sold_items, -> {where("buyer_id is not NULL") }, foreign_key: "saler_id", 6 7end
#Controller
ruby
1class ItemsController < ApplicationController 2 3def show 4 @item = Item.find(params[:id]) 5 if params[:buy] 6 @item.attributes = { 7 buyer: current_user.id 8 } 9 redirect_to "/items/index" 10 end 11end
遷移先のページをまだ作っていないので、とりあえず遷移先は商品の一覧ページにしました。
#Views
ruby
1 <%= button_to "この商品を購入する", { :action => "show", :id => @item.id }, name:"buy", :confirm => "この商品を購入しますか?", class: "btn btn-danger"%>
どうやったらボタンからデータベースに値を入力し、ページ遷移ができるようになりますか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。