create時、ストロングパラメータがあるのにpermitted: falseが出る。
Q&A
Closed
解決したいこと
parameterを正常に受け取り
購入ボタンを押すと、購入することができる
例)
Ruby on Railsで購入機能の実装を行っています
購入機能の実装中にエラーが発生しました。
解決方法を教えて下さい。
発生している問題・エラー
https://gyazo.com/4cd36191e039c81f5ea393f487eb735f
ActionController::ParameterMissing in OrdersController#create
param is missing or the value is empty: order_payment
該当するソースコード
class OrdersController < ApplicationController
before_action :set_item, only: [:index, :create]
def index
end
def create
binding.pry
@order_payment= OrderPayment.new(order_params)
if @order_payment.valid?
pay_item
@order_payment.save
redirect_to root_path
else
render :index
end
end
private
def set_item
@item = Item.find(params[:item_id])
end
def order_params
params.require(:order_payment).permit(:post_code, :prefecture_id, :city, :block, :building, :phone_number)
.merge(user_id: current_user.id, item_id: @item_id, token: params[:token])
end
def pay_item
Payjp.api_key = ENV["PAYJP_SECRET_KEY"]
Payjp::Charge.create(
amount: @item.price,
card: order_params[:token],
currency:'jpy'
)
end
end
<%= form_with model: @order_payment, url: item_orders_path, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>
class OrderPayment < ApplicationRecord
include ActiveModel::Model
attr_accessor :user_id, :item_id, :post_code, :prefecture_id, :city, :block, :building, :phone_number, :token
with_options presence: true do
validates :user_id
validates :item_id
validates :post_code, format: { with: /\A[0-9]{3}-[0-9]{4}\z/, message: 'is invalid. Include hyphen(-)' }
validates :prefecture_id, numericality: { other_than: 0, message: "can't be blank" }
validates :city
validates :block
validates :phone_number, format: { with: /\A\d{10,11}\z/, message: 'is invalid' }
validates :token
end
def save
order = Order.create(user_id: user_id, item_id: item_id)
Payment.create(order_id: order.id, post_code: post_code, prefecture_id: prefecture_id,
city: city, block: block, building: building, phone_number: phone_number, order_id: order.id)
end
end
#<ActionController::Parameters {"authenticity_token"=>"ATUyhRboNHSp0gbbyg35UvRAiUcmIiWXBCJzGffYDnsQhhUSQ8feeW-kcj3e4d4rrAtz_AJ-piz3oD0iN7Q9TQ", "card_number"=>"", "exp_mouth"=>"", "exp_year"=>"", "cvc"=>"", "post_code"=>"", "prefecture_id"=>"0", "city"=>"", "block"=>"", "building"=>"", "phone_number"=>"", "commit"=>"購入", "controller"=>"orders", "action"=>"create", "item_id"=>"5"} permitted: false>
### 自分で試したこと
パラメーターの受け取りがうまくいかないことから、
フォームの見直し、インスタンスの見直しをしたところ、
問題ないと思っています。
他に考えられる箇所があれば、教えていただきたいです
0 likes