在运行方法后获取模型以保存

时间:2015-06-01 18:54:05

标签: ruby-on-rails model-view-controller

我有一个模型,可以创建并运行多个API调用来填充字段。我可以让API调用在我的控制台中正常运行,但是当我尝试在应用程序中运行它时它不会保存,我不确定它是否运行该方法。

这是模型......

transaction.rb

  field :transactions
  field :access_token
  field :public_token
  field :accounts

  def token_exchange
    exchangeTokenResponse = API.exchange_token(public_token)
    self.access_token = exchangeTokenResponse.access_token
    self.accounts = API.set_user(access_token, ['auth'])
    self.transactions = API.set_user(access_token, ['connect'])
  end
end

我的控制器就在这里......

transactions_controller.rb

def exchange_token
    current_tenant.create_transaction(:public_token => params[:public_token])
    current_user.transaction.token_exchange
    redirect_to ...
  end

end

当调用exchange_token路由时,它应该在模型中运行方法,将响应保存到模型中的相应字段,然后重定向到下一页。我不能让它工作,因为它似乎没有保存或我不确定它甚至运行方法,但当我把一个加注项目,它表明它确实通过该方法。知道我做错了吗?

我的控制台在运行后显示此内容

Started GET "/users/transactions/exchange_token?public_token=..." for 10.0.2.2 at 2015-06-01 18:47:58 +0000
Processing by Users::TransactionsController#exchange_token as HTML
  Parameters: {"public_token"=>"..."}
D, [2015-06-01T18:47:59.477157 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.6326ms
D, [2015-06-01T18:47:59.478691 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=lease_defend_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('....')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.7417ms
D, [2015-06-01T18:47:59.495108 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=development collection=transactions selector={"$query"=>{"user_id"=>BSON::ObjectId('...')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.4533ms
D, [2015-06-01T18:47:59.497133 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 DELETE       database=development collection=transactions selector={"_id"=>BSON::ObjectId('...')} flags=[:remove_first]
D, [2015-06-01T18:47:59.497519 #3521] DEBUG -- :                          COMMAND      database=development command={:getlasterror=>1, :w=>1} runtime: 0.4444ms
D, [2015-06-01T18:47:59.499636 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 INSERT       database=development collection=transactions documents=[{"_id"=>BSON::ObjectId('...'), "public_token"=>"...", "user_id"=>BSON::ObjectId('...')}] flags=[]
D, [2015-06-01T18:47:59.500036 #3521] DEBUG -- :                          COMMAND      database=development command={:getlasterror=>1, :w=>1} runtime: 0.3621ms
Redirected to http://0.0.0.0:3000/

编辑 - 我收到了这个回复,我需要把它放到一个字段中以便稍后调用。什么是哈希?阵列?或者是......

#<Api::User:0xb7e9f2c @accounts=[#<Api::Account:0xb7b6eb0 @id="QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", @name=nil, @type="depository", @meta={"number"=>"9606", "name"=>"Bank Savings"}, @institution_type="fake_institution", @available_balance=1203.42, @current_balance=1274.93, @subtype=nil, @numbers={"routing"=>"021000021", "account"=>"9900009606", "wireRouting"=>"021000021"}>, #<Api::Account:0xb7b6ba4 @id="nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", @name=nil, @type="depository", @meta={"number"=>"1702", "name"=>"Bank Checking"}, @institution_type="fake_institution", @available_balance=1081.78, @current_balance=1253.32, @subtype=nil, @numbers={"routing"=>"021000021", "account"=>"9900001702", "wireRouting"=>"021000021"}>, #<Api::Account:0xb7b68e8 @id="XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", @name=nil, @type="depository", @meta={"number"=>"5204", "name"=>"Bank Premier Checking"}, @institution_type="fake_institution", @available_balance=7205.23, @current_balance=7255.23, @subtype=nil, @numbers={"routing"=>"021000021", "account"=>"9900005204", "wireRouting"=>"021000021"}>, #<Api::Account:0xb7b66cc @id="pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", @name=nil, @type="credit", @meta={"limit"=>12500, "number"=>"3002", "name"=>"Bank Credit Card"}, @institution_type="fake_institution", @available_balance=9930, @current_balance=2275.58, @subtype=nil, @numbers={}>], @transactions=[], @permissions=["auth"], @access_token="test_chase", @api_res="success", @info={}, @pending_mfa_questions="", @type=nil>

1 个答案:

答案 0 :(得分:2)

问题是,在调用方法token_exchange后没有调用任何保存,重写方法如下,它将保存您的事务

if let window = NSApplication.sharedApplication().mainWindow {
        if let undoManager = window.undoManager {
            undoManager.registerUndoWithTarget(self, selector: Selector("removeLatestEntry:"), object: "test")
        }
    }