gpt4 book ai didi

ruby-on-rails - 注销应用程序的问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:40 25 4
gpt4 key购买 nike

我在退出应用程序时遇到问题。当我单击“注销”时,出现错误“undefined method` id 'for nil: NilClass。”登录时一切正常。错误显示在第 9 行。

 @search = Expense.where(:user_id => current_user.id).search(params[ :q ])

请帮忙。

费用 Controller .rb


class ExpensesController < ApplicationController
before_action :user_id, only: [:show, :edit, :update, :destroy]

# GET /expense expense
# GET /expense.json


def index
@search = Expense.where(:user_id => current_user.id).search(params[ :q ])
@expense = @search .result
@search.build_condition
end

# GET /expense/1
# GET /expense/1.json
def show

end

# GET /expense/new
def new
@expense = Expense.new
end

# GET /expense/1/edit
def edit
end

# POST /expense
# POST /expense.json
def create
@expense = Expense.new(expense_params)
@expense.user_id = current_user.id if current_user
respond_to do |format|
if @expense.save
format.html { redirect_to @expense, notice: 'zostały zapisane.' }
format.json { render :show, status: :created, location: @expense }
else
format.html { render :new }
format.json { render json: @expense.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /expense/1
# PATCH/PUT /expense/1.json
def update
respond_to do |format|
if @expense.update(expense_params)
format.html { redirect_to @expense, notice: 'expense was successfully updated.' }
format.json { render :show, status: :ok, location: @expense }
else
format.html { render :edit }
format.json { render json: @expense.errors, status: :unprocessable_entity }
end
end
end

# DELETE /expense/1
# DELETE /expense/1.json
def destroy
@expense.destroy
respond_to do |format|
format.html { redirect_to @expense, notice: 'Zakupy zostały usunięte.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_expense
@expense = Expense.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def expense_params
params.require(:expense).permit(:date, :price, :category, :where)
end
end

架构

ActiveRecord::Schema.define(version: 2019_08_26_203551) do

create_table "expenses", force: :cascade do |t|
t.date "date"
t.decimal "price"
t.string "category"
t.string "where"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["user_id"], name: "index_expenses_on_user_id"
end

create_table "searches", force: :cascade do |t|
t.date "min_date"
t.date "max_date"
t.string "category"
t.decimal "min_price"
t.decimal "max_price"
t.string "where"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "users", 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_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

end

路线

Rails.application.routes.draw do
devise_for :users

root 'expenses#index'
get 'search/search'
get 'expenses/search'
resources :expenses
end

最佳答案

您在查询中使用 current_user.id。一旦你注销 current_user 是 nil,所以你不能调用它的 id

您有多种选择:

  • 仅限登录用户访问费用
  • 有条件地调用当前用户:user_id = current_user&.id
  • 提供默认访客用户:
def current_user_with_default
current_user || GuestUser.new
end

关于ruby-on-rails - 注销应用程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57699401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com