Rails destroy 动作不删除
Rails destroy action doesn't delete
我想将服务对象添加到我的控制器中。然而 destroy 操作似乎没有正常工作 - 基本上它不会删除任何东西只是重定向到页面而没有 flash 消息。
user_stock_destroyer.rb
class UserStocksDestroyer
def initialize(current_user, params, flash)
@current_user = current_user
@params = params[:id]
@flash = flash
end
def call
stock = Stock.find(params)
@user_stock = UserStock.where(user_id: current_user.id, stock_id: stock.id).first
@user_stock.destroy!
flash[:notice] = 'Stock successfully removed'
end
private
attr_reader :current_user, :params, :flash
end
user_stocks_controller.rb
class UserStocksController < ApplicationController
def destroy
UserStocksDestroyer.new(current_user, params, flash)
redirect_to my_portfolio_path
end
end
您正在创建对象,但没有调用 call
执行工作的方法
def destroy
UserStocksDestroyer.new(current_user, params, flash).call
redirect_to my_portfolio_path
end
我想将服务对象添加到我的控制器中。然而 destroy 操作似乎没有正常工作 - 基本上它不会删除任何东西只是重定向到页面而没有 flash 消息。
user_stock_destroyer.rb
class UserStocksDestroyer
def initialize(current_user, params, flash)
@current_user = current_user
@params = params[:id]
@flash = flash
end
def call
stock = Stock.find(params)
@user_stock = UserStock.where(user_id: current_user.id, stock_id: stock.id).first
@user_stock.destroy!
flash[:notice] = 'Stock successfully removed'
end
private
attr_reader :current_user, :params, :flash
end
user_stocks_controller.rb
class UserStocksController < ApplicationController
def destroy
UserStocksDestroyer.new(current_user, params, flash)
redirect_to my_portfolio_path
end
end
您正在创建对象,但没有调用 call
执行工作的方法
def destroy
UserStocksDestroyer.new(current_user, params, flash).call
redirect_to my_portfolio_path
end