railsにてpermitted: falseの解消法
解決したいこと
Ruby on Railsで商品購入機能のあるWebアプリをつくっています。
payjpのcheckoutを使用しているのですが、購入がうまくいきません。
解決方法を教えて下さい。
発生している問題・エラー
[1] pry(#<OrdersController>)> order_params
=> #<ActionController::Parameters {"rank_id"=>"18", "character"=>["オクタン"], "play_style"=>"a", "play_time"=>"a", "play_device"=>["Xbox"], "communication_tool"=>["OpenVC"], "goal"=>"a", "user_id"=>2, "coach_id"=>"1", "token"=>"tok_86acf73f77da4ea019252ac66eda"} permitted: true>
[2] pry(#<OrdersController>)> params
=> #<ActionController::Parameters {"authenticity_token"=>"Pqk16eRg2BqoGR-r7PpZu-syUIBDqaiYgDfKt4IqsOftpNgUnyLVGqS_L0vfXV4YsO2FLxpNmwtt8HjkWvGXkw", "payjp-token"=>"tok_86acf73f77da4ea019252ac66eda", "order_form"=>#<ActionController::Parameters {"rank_id"=>"18", "character"=>["オクタン"], "play_style"=>"a", "play_time"=>"a", "play_device"=>["Xbox"], "communication_tool"=>["OpenVC"], "goal"=>"a"} permitted: false>, "commit"=>"購入", "controller"=>"orders", "action"=>"create", "coach_id"=>"1"} permitted: false>
[3] pry(#<OrdersController>)> @order_form.valid?
=> true
該当するソースコード
class OrdersController < ApplicationController
before_action :authenticate_user!
before_action :non_matched_coach, only: [:index, :create]
def index
@order_form = OrderForm.new
end
def create
@order_form = OrderForm.new(order_params)
#binding.pry
if @order_form.valid?
@order_form.save
redirect_to root_path
else
render :index
end
end
private
def order_params
params.require(:order_form).permit(:rank_id, {:character => []}, :play_style, :play_time, {:play_device => []}, {:communication_tool => []}, :goal).merge(user_id: current_user.id, coach_id: params[:coach_id], token: params["payjp-token"])
end
def non_matched_coach
@coach = Coach.find(params[:coach_id])
if current_user.id == @coach.user_id
redirect_to root_path
end
end
end
class OrderForm
include ActiveModel::Model
attr_accessor :user_id, :coach_id, :rank_id, :character, :play_style, :play_time, :play_device, :communication_tool, :goal, :token
with_options presence: true do
validates :user_id
validates :coach_id
validates :rank_id
validates :character
validates :play_style
validates :play_time
validates :play_device
validates :communication_tool
validates :goal
validates :token
end
def save
order = Order.create(user_id: user_id, coach_id: coach_id)
Payment.create(coach_id: coach_id, order_id: order.id, rank_id: rank_id, character: character, play_style: play_style, play_time: play_time, play_device: play_device, communication_tool: communication_tool, goal: goal)
end
end
class CreatePayments < ActiveRecord::Migration[6.0]
def change
create_table :payments do |t|
t.references :coach, null: false, foreign_key: true
t.references :order, null: false, foreign_key: true
t.integer :rank_id, null: false
t.text :character, null: false
t.string :play_style, null: false
t.string :play_time, null: false
t.text :play_device, null: false
t.text :communication_tool, null: false
t.text :goal, null: false
t.timestamps
end
end
end
<%= form_with model: @order_form, url: coach_orders_path(@coach), id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>
自分で試したこと
購入しようとするとtoken can't be blankと出るので、トークンがバリデーションで認識されていないのだと思い、binding.pryをかけた。
payjp-tokenは発行されているが、permitted: falseと出てしまっていたので、mergeの記述を変更した。
order_paramsはtrueだが、paramsではfalseになっていて購入がうまくいっていないように見える。
0