Ruby不会迭代Google自定义搜索响应项

Ruby不会迭代Google自定义搜索响应项,ruby,sinatra,google-custom-search,google-api-client,Ruby,Sinatra,Google Custom Search,Google Api Client,我正在尝试创建一个简单的应用程序,它使用Google自定义搜索API和Sinatra。代码如下: require 'rubygems' require 'sinatra' require 'google/api_client' def extract_cse_info(thekey) client = Google::APIClient.new(:key => 'my_api_key', :authorization => nil) search = client.

我正在尝试创建一个简单的应用程序,它使用
Google自定义搜索API
Sinatra
。代码如下:

require 'rubygems'
require 'sinatra'
require 'google/api_client'

def extract_cse_info(thekey)
    client = Google::APIClient.new(:key => 'my_api_key', :authorization => nil)
    search = client.discovered_api('customsearch')

    response = client.execute(
        :api_method => search.cse.list,
        :application_name => 'Sgoogle',
        :application_version => '0.1',
        :parameters => {
            'q' => thekey,
            'key' => 'my_api_key',
            'cx' => 'my_cx_id'
        }
    )
    return JSON.parse(response.body)
end


get '/' do 
    erb :index
end

post '/ssearch' do

    keyword = params[:key]

    items = extract_cse_info(keyword)

    erb :ssearch, :locals => { 'items' => items }
end
索引视图正确运行,并将数据传递给/ssearch视图(我也在irb上测试了
GoogleCSEAPI
,它完成了搜索)。但是我不知道如何在items变量中显示链接内容。我只需要搜索结果的链接,我将在以后使用

这是/s搜索视图:

<h3><%= items["items"].each do |x| %>
    <%= x["links"] %>
    <%= end %>
</h3>

但服务器返回:

views/ssearch.erb:1:语法错误,意外'' …[“项目”][“链接”]。每一个都做(x)到(美国)@_外螺纹接头“\n”^

我会理解如何正确迭代这些项目,有人能帮我吗?奇怪的事情(对我来说,因为我确信我的错误并不奇怪,可能是因为我的新手)是如果我编写以下代码:

<h3><%= items["items"][0]["link"] %></h3>

我得到了正确的结果(但显然只针对0索引)。

不起作用(也没有意义),请使用


添加到该文件中。当您使用
时,它会尝试显示值。这就是为什么它试图调用
来访问它的
。使用
可以执行迭代集合或使用
if
语句等操作。
<% items["items"].each do |x| %>
  <%= x["links"] %>
<% end %>