【问题标题】:Rails helper loops html is appearing on my viewRails helper loops html 出现在我的视图中
【发布时间】:2017-05-14 04:25:06
【问题描述】:

嗨,所以我在视图上使用了一个帮助模块来运行一些功能...... 但不知何故,这部分帮助代码出现在我的 html 中。

for i in 0..minedTextSize

这是在 html 上

这是调用辅助方法的html。

<h1>TEST TEST</h1>
<%=extract_value("filePath")%>

在我的控制器中,我只包含了辅助方法

class MainPageController < ApplicationController
  include ExtractHelper

  def index
  end
end

有什么想法吗?

更新########

这是我的助手 ruby​​ 代码

module ExtractHelper
  require 'rubygems'
  require 'nokogiri'
  require 'open-uri'
  require 'pdf-reader-turtletext'

  def download_pdf(url)
    found = false
    page = Nokogiri::HTML(open("http//www.somesite.com")
    puts page.class   # => Nokogiri::HTML::Documents

    for i in 0..(page.css('a').size - 1)
      if(page.css('a')[i]['href'][-4,4] != ".pdf")
        next
      else
        found = true
        file_link = page.css('a')[i]['href']
        file_link.gsub!(' ','%20')
        link = "http//www.somesite.com/" + file_link # Generate the $
        puts link
        download = open(link)

       IO.copy_stream(download,"/home/ec2user/my_project/pdfFiles/Demo.pdf")
        puts file_link + " has been downloaded."
        @link = file_link + " has been downloaded."
        if found == true
          break
        end
      end  #if
    end     # for
  end    #def


  def extract_value(filePath)
    filePath = "/home/ec2-user/my_project/pdfFiles/Demo.pdf"
    reader = PDF::Reader::Turtletext.new(filePath)

    textangle = reader.bounding_box do
      inclusive true
      page 4
      below "RIGHT HERE"
    end

    minedText = textangle.text
    minedTextSize = minedText.size - 1

    for i in 0..minedTextSize
      if minedText[i][0].include? "WEEK 1"
        puts minedText[i][1]
        @lastweek = minedText[i][1]
      end
      if minedText[i][0].include? "WEEK 2"
        puts minedText[i][1]
        @previousweek = minedText[i][1]
      end
    end
  end  #def

end     #module

【问题讨论】:

  • 能把ExtractHelper的内容加进去吗?
  • 你能补充更多细节吗?
  • 添加了上面的帮助代码。

标签: html ruby-on-rails ruby helper


【解决方案1】:

Puts 打印到 stdout。您在视图中得到 0..36,因为助手正在返回其最后评估的行(a for)的值。尝试明确评估/返回某些东西,例如:

def extract_value(filePath)
  # some initial code
  value = ''
  for i in 0..minedTextSize
    # some more code
    value = 'something'
  end
  "String concatenating some #{value}"
end

【讨论】:

  • 所以......即使我从未指定它,它实际上也会打印出 for 语句?这很奇怪
  • 它正在打印您在 for 中使用的范围的文本表示。在 irb 中尝试for i in 0..36; end。即使 for 为空,它也会返回原始范围。在您的助手中显式返回一些值(例如,查看我编辑的答案)可能会有所帮助。
【解决方案2】:

您的IO.copy_stream 中有不匹配的引号,其中包含/home/ec2user/my_project/pdfFiles/Demo.pdf。在相同的方法中,您也有一个没有结束括号的开始括号。

【讨论】:

  • 我的错。让我解决这个问题。但这不是问题。
猜你喜欢
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 2012-11-16
相关资源
最近更新 更多