Html 如何在RubyonRails中打印每一行

Html 如何在RubyonRails中打印每一行,html,ruby-on-rails,frontend,Html,Ruby On Rails,Frontend,我将从oracle server查询数据。下面是我得到的,我想把每一行都打印出来。但它看起来像“只打印最后一个结果”,而“不打印任何结果”。我很困惑。 我确信我连接到了oracle服务器,在本例中我要打印的是所有10个国家的名称 这是my helper.rb文件 require 'ruby-oci8' module HomeHelper def connect oci=OCI8.new('username','password','server/host')

我将从oracle server查询数据。下面是我得到的,我想把每一行都打印出来。但它看起来像“只打印最后一个结果”,而“不打印任何结果”。我很困惑。 我确信我连接到了oracle服务器,在本例中我要打印的是所有10个国家的名称

这是my helper.rb文件

require 'ruby-oci8'
module HomeHelper
    def connect
        oci=OCI8.new('username','password','server/host')
        oci.exec('select name from country fetch first 10 rows only') do |record|
                puts record.join(',')
            end
    end
end
这是我的html.erb文件

<%= connect %>

在ERB模板中,有两种执行ruby代码的基本方法

(no=)用于嵌入逻辑
(with=)用于嵌入

要实现你想要的,你需要两者的结合。类似的内容将在您的视图中显示它们:

<% collection.each do |entry| %>
  <%= entry.name %>
<% end %>

在ERB
中,puts
print
不会像您预期的那样实际写入缓冲区。考虑这个例子:

require 'erb'

def connect
  puts "hello world"
end

template = ERB.new "<% connect %>"
template.result(binding) # "\n"
如果绝对必须在非输出代码块(即)内输出文本,则可以使用:

但实际上,这段代码首先更属于模型(或类似于存储库类的东西),而不是助手:

require 'ruby-oci8'

class Country < Struct.new(:name)
  @oci= OCI8.new('username','password','server/host')

  def self.top_ten
    [].then do |results|
      @oci.exec('select name from country fetch first 10 rows only') do |record|
        results << new(record)
      end
    end
  end
end
需要“ruby-oci8”
类别国家/地区结果收集和输入的内容应该是什么?我没有任何线索。
收集
在这种情况下将是一个数组
entry
是数组值的变量表示形式,
entry.name
将调用
entry
上的
name
方法。我想通读一遍,因为您需要设置
@instance\u variables
,以便将数据公开给您的视图。第一种方法有效,谢谢兄弟。对于第二种方法,存在一个错误,即类国家的超类不匹配。这是否意味着“Struct.new(:name)”不正确?
require 'ruby-oci8'
module HomeHelper
  def connect
    oci = OCI8.new('username','password','server/host')
    oci.exec('select name from country fetch first 10 rows only') do |record|
     concat record.join(',')
    end
  end
end
require 'ruby-oci8'

class Country < Struct.new(:name)
  @oci= OCI8.new('username','password','server/host')

  def self.top_ten
    [].then do |results|
      @oci.exec('select name from country fetch first 10 rows only') do |record|
        results << new(record)
      end
    end
  end
end
<%= Country.top_ten.map(&:name).join(', ') %>