@POSO

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

before action

解決したいこと

もし商品が購入されていた場合、詳細を見る以外のこと(edit,update,destroy)を弾くという処理をしたい

該当するソースコード

class OrdersController < ApplicationController
before_action :authenticate_user!
before_action :noaction, only: [:index, :create]
before_action :honnin, only: [:index, :create]

def index
@furima = Furima.find(params[:furima_id])
if current_user = @furima_user
redirect_to root_path
else
@furima_order = FurimaOrder.new
end
end

def create
@furima = Furima.find(params[:furima_id])
@furima_order = FurimaOrder.new(order_params)
@furima = Furima.find(params[:furima_id])
if @furima_order.valid?
pay_furima
@furima_order.save
return redirect_to root_path
else
render :index
end
end

private

def order_params
  params.require(:furima_order).permit(:yubin, :sityoson, :banti, :tatemono, :phone, :basyo_id).merge(token: params[:token], furima_id: params[:furima_id], user_id: current_user.id )
end

def pay_furima
Payjp.api_key = ENV["PAYJP_SECRET_KEY"]
Payjp::Charge.create(
amount: @furima.price,
card: order_params[:token],
currency: 'jpy'
)
end

def noaction
@furima = Order.find(params[:furima_id])
redirect_to root_path
end
def noedit

end

def honnin
@furima = Furima.find(params[:furima_id])
redirect_to root_path unless current_user == @furima.user
end

end

自分で試したこと

befoactionなどでedit自体を弾く処理をしましたが、これだと普通に編集しようとしても弾かれます

0 likes

1Answer

意図が正しく伝わってなかったらすみません.

befoactionなどでedit自体を弾く処理をしましたが、これだと普通に編集しようとしても弾かれます

とあるので,現状を確認すると,

noaction に分岐が無いので常に,redirect_to root_path しているのではないでしょうか?

例えば本人のみ可能にする場合はその分岐が必要かもしれません.

0Like

Your answer might help someone💌