如何从视图RAILS 4.0调用控制器方法

时间:2013-08-10 23:56:09

标签: ruby-on-rails api view controller

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

我的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放入我的Surveys视图中。我可能不需要Survey.create(y: response_count),但这是我试图让它工作的一个尝试。基本上,我只想在视图中写这个:

puts response_count

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

1 个答案:

答案 0 :(得分:0)

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

@response_count = response["count"]

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

<%= @response_count %>