0

我想我正确地说明了这个问题的标题,但这就是问题所在:

我的 SurveysController 中有以下内容-

  def pull_responses
    call= "/api/v2/surveys/"
    auth = {:username => "test", :password => "password"}
    url = HTTParty.get("https://fluidsurveys.com#{call}",
                       :basic_auth => auth,
                       :headers => { 'ContentType' => 'application/json' } )
    response = JSON.parse(url.body)
    survey_ids = response["surveys"].map { |s| s["id"] }

    survey_ids.each do |x|

      call= "/api/v2/surveys/#{x}/responses/?_completed=1"
      auth = {:username => "test", :password => "password"}
      url = HTTParty.get("https://Fluidsurveys.com#{call}",
                         :basic_auth => auth,
                         :headers => { 'ContentType' => 'application/json' } )

      response = JSON.parse(url.body)
      response_count = response["count"]
      Survey.create(y: response_count)
    end
  end
  helper_method :pull_responses

我要做的就是将变量response_count放在我的调查视图中。我可能不需要Survey.create(y: response_count),但这是我尝试让它发挥作用的一部分。本质上,我只想在一个视图中写这个:

puts response_count

将响应放入视图的正确方法是什么?

4

1 回答 1

0

您需要将值分配给实例变量(通过@在名称前添加),以便它将持续存在于视图中:

@response_count = response["count"]

然后在视图中你可以像这样打印它:

<%= @response_count %>
于 2013-08-11T00:03:15.430 回答