taikinakano
@taikinakano (taiki nakano)

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!

論理削除機能の実装時エラー

論理削除機能の実装

no method error

現在、Ruby on RailsでECサイトの制作をしています。
論理削除機能を実装しようとしているのですが、削除ボタンを押すとこのようなエラーが発生してしまいます。
解決方法を教えて下さい。

スクリーンショット 2021-06-18 9.47.41.png

ECサイトのコード

class Public::CustomersController < ApplicationController
    def show
      @customer = Customer.find(params[:id])
    end

    def withdraw_confirm
    end

    def withdraw
    @customer = current_customer
    @customer.update(is_deleted: "Available")
    reset_session
    redirect_to root_path
    end

    def edit
      @customer = current_customer
    end

    def update
      @customer = current_customer
      if @customer.update(customer_params)
       flash[:notice] = "登録を変更しました。"
       redirect_to customer_path(@customer)
      else
       render "edit"  
      end
    end  
  private
  def customer_params
    params.require(:customer).permit(:last_name, :first_name, :last_name_kana, :first_name_kana, :email, :password, :postal_code, :address, :telephone_num, :is_deleted, :created_at, :updated_at )
  end

end

routes.rb

Rails.application.routes.draw do
  root to: 'homes#top'
  get 'homes/about' => 'homes#about', as: 'about'


  devise_for :customers, controllers: {
    registrations: 'public/customers/registrations',
    sessions: "public/customers/sessions",
    passwords: 'public/customers/passwords'
  }

  namespace :admin do
    devise_for :admins, controllers: {
      sessions: 'admin/admins/sessions',
      registrations: 'admin/admins/registrations',
      passwords: 'admin/admins/passwords'
    }
  end 

  namespace :public do
    get '/customer/confirm' => 'customers#withdraw_confirm', as: 'confirm_withdraw'
    patch '/customers/withdraw' => 'customers#withdraw', as: 'withdraw_customer'
    resources :products, only: [:index, :show]
    resources :customers, only: [:show, :edit, :update]
  end
end

customers.rb

class Customer < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  validates :last_name,        presence: true
  validates :first_name,       presence: true
  validates :last_name_kana,   presence: true
  validates :first_name_kana,  presence: true
  validates :email,            presence: true,
                               uniqueness: true,
                               format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i}
  validates :postal_code,      presence: true
  validates :address,          presence: true
  validates :telephone_number, presence: true
  validates :password,         presence: true


   enum is_deleted: {Available: true, Invalid: false} 

    def active_for_authentication?
        super && (self.is_deleted === "Invalid")
    end
end

schema.rb

ActiveRecord::Schema.define(version: 2021_06_16_022617) do

  create_table "admins", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_admins_on_email", unique: true
    t.index ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true
  end

  create_table "customers", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.string "last_name", null: false
    t.string "first_name", null: false
    t.string "last_name_kana", null: false
    t.string "first_name_kana", null: false
    t.string "postal_code", null: false
    t.string "address", null: false
    t.string "telephone_number", null: false
    t.boolean "is_deleted", default: false, null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_customers_on_email", unique: true
    t.index ["reset_password_token"], name: "index_customers_on_reset_password_token", unique: true

ActiveRecord::Schema.define(version: 2021_06_16_032430) do

  create_table "products", force: :cascade do |t|
    t.integer "genre_id"
    t.string "name"
    t.string "image_id"
    t.text "introduction"
    t.integer "price"
    t.boolean "is_active", default: true, null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end


自分で試したこと

モデルでのenumの定義が間違っているのかと思ったのですが、違うようなのでわかりませんでした。
以下のようなサイトを参考にしました。
https://qiita.com/tt_tsutsumi/items/588d0e65a289a15530d2

0

2Answer

エラーはundefined method 'update' for nil:NilClassなので、まずは@customer(current_customer)が何らかの理由でnilになっていないか確認してみてはいかがでしょうか。

ググってみるとsign_inをやめてbypass_sign_inを使うという話があって、もしかしたら関係あるかもです。
https://teratail.com/questions/125888

1Like

Your answer might help someone💌