LoginSignup
1
0

More than 3 years have passed since last update.

modelではdeviseで利用できるcurrent_userは使えない

Posted at

modelではcurrent_userは使えない

teratail
https://teratail.com/questions/272877

変数やmethodはそれが参照可能な範囲があります。

「変数やmethodはそれが参照可能な範囲があります。current_user はmethodです。paramsもHashの様な顔をしてますが、振る舞いをみるとmethodっぽいです。methodは 単独で使う場合は、それが定義されているインスタンスでないと参照できません。これらはcontrollerのインスタンスのものなので、modelでは参照できません。
必要なときは、modelのmethodを呼ぶときに引数で渡す必要があります。」

なのでコントローラーで使う

だからmodelにおいたaddress_purchase.rbで以下のようにコーディングしました。

app/models/address_purchase.rb
  def item_params
    params.require(:item).permit(:item_name, :description, :category_id, :status_id, :delivery_fee_payment_id, :prefecture_id, :delivery_prepare_id, :price, :image).merge(user_id: current_user.id)
  end

モデル内ではうまく動きませんでした。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0