2016-08-04 12 views
-1

我模塊mangopay_helper.rb如何在ruby模塊中連接flash [:notice]?

module MangoPayHelper 

    def transfer_to_contractor(contract, client, contractor) 
    amount = contract.amount * 0,1.to_i 
    begin 
    MangoPay::Transfer.create({ 
     Tag: "#{contract.id} + #{contract.title}", 
     AuthorId: client.mangopay_id, 
     DebitedFunds: {Currency: "EUR", Amount: contract.amount}, 
     Fees: { Currency: 'EUR', Amount: amount}, 
     DebitedWalletId: client.wallet_id, 
     CreditedWalletId: contractor.wallet_id 
    }) 
    rescue MangoPay::ResponseError => e 
    flash[:notice] = " Code: #{ e['Code'] } Message: #{ e['Message'] }" 
    end 
    end 
end 

在控制器的動作我叫MangoPayHelper.transfer_to_contractor

Error: undefined local variable or method `flash' for MangoPayHelper:Module 

如何在紅寶石模塊中連接閃存?

+0

通閃光燈作爲你爲什麼想從助手修改'flash' transfer_to_contractor' –

+0

但是方法'的論點?這不是好的方法,控制器是使用'flash'玩的正確地方。 –

+0

我從模塊中刪除了閃存,但同樣的錯誤。 – dev85

回答

0

你可以(也應該)讓控制器處理flash哈希的操作。

module MangoPayHelper 
    def self.transfer_to_contractor!(contract, client, contractor) 
    amount = contract.amount * 0,1.to_i 

    MangoPay::Transfer.create({ 
     Tag: "#{contract.id} + #{contract.title}", 
     AuthorId: client.mangopay_id, 
     DebitedFunds: {Currency: "EUR", Amount: contract.amount}, 
     Fees: { Currency: 'EUR', Amount: amount}, 
     DebitedWalletId: client.wallet_id, 
     CreditedWalletId: contractor.wallet_id 
    }) 
    end 
end 

class TransfersController < ApplicationController 
    def create 
    # [...] 
    begin 
     MangoPayHelper.transfer_to_contractor!(contract, client, contractor) 
    rescue MangoPay::ResponseError => e 
     flash[:notice] = " Code: #{ e['Code'] } Message: #{ e['Message'] }" 
    end 
    # [...] 
    end 
end 
+0

我undestand。謝謝你的回答 – dev85

0

首先,撥打MangoPayHelper.transfer_to_contractor與你的代碼不應該工作,因爲你有def transfer_to_contractor - 你需要def self.transfer_to_contractor使它可以這樣調用。

flash是一種控制器方法。爲了使它可以從模塊中調用,您可以在控制器中使用include模塊,然後調用方法transfer_to_contractor而不是MangoPayHelper.transfer_to_contractor。當你包含一個模塊時,模塊可以訪問包含者的方法,如flash

例如,

class SomeController < ApplicationController 
    include MongoPayHelper 
    def my_route 
    transfer_to_controller(some args) 
    etc 
    end 
end 
+0

謝謝,未初始化的常量ContractsController :: MangoPayHelpererror。模塊存儲在'lib'文件夾中 – dev85

+0

現在好吧,這是關於您的模塊定義在哪裏的錯誤。嘗試在配置/初始化文件夾中的文件 –

相關問題