我想将服务对象添加到我的控制器中。是否有机会在此服务对象中包含闪存消息?
user_stocks_controller
class UserStocksController < ApplicationController
def create
@user_stock = UserStocksCreator.new(current_user, params).call
redirect_to my_portfolio_path
end
end
服务对象user_stocks_creator
class UserStocksCreator
def initialize(current_user, params)
@current_user = current_user
@params = params[:stock_ticker]
end
def call
stock = Stock.find_by_ticker(params)
if stock.blank?
stock = Stock.new_from_lookup(params)
stock.save
end
@user_stock = UserStock.create(user: current_user, stock: stock)
flash[:success] = "Stock #{@user_stock.stock.name} was successfully added to portfolio"
end
private
attr_accessor :current_user, :params
end
使用此代码,我有一个错误:
未定义的局部变量或方法“flash”