1

我正在为 Interactor gem 中的一个小功能而苦苦挣扎(来自我们在 Collective idea 的朋友)。

我想要做的是在我的交互器中显示一条消息,就像在我的控制器中一样。

这是我的控制器中的 create 方法,我在其中实例化了我的交互器:

def create
    buy_credits = BuyCredit.new(send_transactions_params)
    buy_credits.perform

    redirect_to profile_path
end

这是我的交互器的构建:

def build_transaction

    if context[:transaction][:recipient_id].nil? && context[:transaction][:recipient_type].nil?

      @transaction = user.transactions.build(context[:transaction])
      # flash[:success] = I18n.t('.bought_credits_for_client', recipient: user)

    elsif context[:transaction][:recipient_id].present? && context[:transaction][:recipient_type] == Organisation.name

      current_organisation = user.organisations.find(context[:transaction][:recipient_id])
      @transaction = current_organisation.transactions.build(context[:transaction])
      # flash[:success] = I18n.t('.bought_credits_for_organisation', recipient: current_organisation)

    else
      # flash[:error] = I18n.t('.cant_buy_credits')
    end

  end

您可以看到我想要的 Flash 消息类型,它们是注释行。

当然,我可以有类似的东西

if interactor.success?
    redirect_to profile_path
else

在我的控制器中,但正如您在我的评论中看到的那样,我有两种“类型”的成功......

谢谢!

4

1 回答 1

1

感谢另一位同事的帮助,我们设法解决了这个问题:我们只需将 flash 对象(在控制器中实例化)传递给 Interactor 接收的上下文。因此,交互器将其消息添加到对象中,然后在控制器中可用。

这是我的上下文构造:

def send_transactions_params
    params_to_send = {
        transaction: transaction_params,
        buyer: current_user,
        flash: flash
    }
    params_to_send
end
于 2014-07-24T07:38:40.813 回答