如何在视图中调用helper中的方法

时间:2015-08-31 11:51:07

标签: ruby-on-rails http

我正在处理财务申请。之前的方法使用了各种存储过程,但我们现在已经开始执行API调用。最初的代码如下:

查看

%table
  %thead
    %tr
      %th.year(rowspan=2) Year
      %th.month(rowspan=2) Month
      %th.accounts{colspan: @customer.accounts.size} Accounts
    %tr
      - fetch_cu_accounts(@customer.id).each do |account|
        %th= account.name
  - @today.year.downto(@today.year - 3) do |year|
    %tbody
      - x = (@today.year == year) ? @today.month : 12
      - x.downto(1) do |month|
        %tr
          - if month == x
            %td{rowspan: x}= year
          %td= Date::MONTHNAMES[month]
          /- @customer.accounts.each do |acct|
          - fetch_cu_accounts(@customer.id).each do |acct|
            - if acct.activity[year] && acct.activity[year][month]
              %td{'data-transactions' => acct.activity[year][month]}
                - stmt = acct.statement(year, month)
                = link_to 'View', url(:statements, :show, id: stmt.to_s)
                = link_to image_tag('/icons/document-pdf.png', alt: 'Download PDF'), url(:statements, :show, id: stmt, format: :pdf), class: :pdf
            - else
              %td 

模型调用活动

def activity
  return @activity if @activity

  @activity = {}

  @db.call('IBAccountActivity', @account_id).each do |row|
    @activity[row['year']] = {} unless @activity[row['year']]
    @activity[row['year']][row['month']] = row['transactions']
  end

  @activity
end

控制器代码:

get :show, map: '/accounts/:id' do
  account_number = params[:id].to_i
  @account = @customer.fetch_account(account_number)    
  render 'accounts/show'
end

新方法使用以下内容:

  def get_call(routePath)
    started_at = Time.now
    enc_url = URI.encode("#{settings.abacus_service_endpoint}#{routePath}")
    uri = URI.parse(enc_url)
    http = Net::HTTP.new(uri.host, uri.port)

    req = Net::HTTP::Get.new(uri.request_uri, get_header_for_abacus_service)     
    resp = http.request(req)
    logger.bench 'SERVICE - GET', started_at, routePath
    return resp if response_ok?(resp)
  end

将使用以下方法:

  def activity
    return @activity if @activity

    @activity = {}

    response = get_call('/Accounts/AccountActivity/' + account_id.to_s)
    response = JSON.parse(response.body)

    @activity = response.map do |row|
      @activity[row['year']] = {} unless @activity[row['year']]
      @activity[row['year']][row['month']] = row['transactions']
    end

    @activity
  end

目前我得到:

undefined method activity  

我想用以下内容替换以前的活动方法,我希望在其他HTTP调用方法当前所在的helpers.rb文件中使用它。如何在没有被调用的时刻调用此方法。

0 个答案:

没有答案