【问题标题】:Controller not accessing helper method控制器不访问辅助方法
【发布时间】:2014-01-03 21:00:19
【问题描述】:

我认为在尝试保存到我的数据库时没有访问我的辅助方法。正在创建一个新的 Airport 实例,但我期望从 API 获得的数据不存在。它应该根据用户在视图中的 form_for 中输入的 IATA 代码引入机场名称。

换句话说,“名称”在我的数据库中始终为零。因此,似乎根本没有使用 API,并且名称从未发送到控制器进行保存,这使我相信由于某种原因没有调用帮助程序。

如果真的被调用了,为什么“name”没有被填写?

这是我的控制器:

class AirportsController < ApplicationController

  include AirportsHelper

  def new
    @airport = Airport.new
  end

  def create
    new_airport = Airport.create(params[:airport])
      if new_airport.errors.empty? 
        create_location(params[:airport][:code]) #should call the create_location method in AirportsHelper
        redirect_to airport_path(new_airport.id) 
      else
        flash[:notice] = new_airport.errors.full_messages
        redirect_to new_airport_path
      end
  end

  def show
    @airport = Airport.find(params[:id])     
  end

end

这是我的帮助文件:

module AirportsHelper
  def create_location(airport_code)
    airport = Airport.find_by_code(airport_code) #looks up db based on arpt code

    result = Typhoeus.get("https://api.flightstats.com/flex/airports/rest/v1/json/iata/#{airport}?appId=[APP ID]&appKey=[APP KEY]")
    result_hash = JSON.parse(result.body)

    result_hash['airports'].each do |airport|
      @airport_name = airport['name']
    end

    Airport.update_attributes(name: @airport_name, airport_id: airport.id)
    Location.create(name: @airport_name, airport_id: airport.id)
    airport.update_attributes(name: @airport_name)
    airport.save

  end
end

这是我的表单(部分内置):

<%= form_for @airport do |f| %>
  <%= f.text_field :city, :placeholder => "City" %> <p>
  <%= f.text_field :country, :placeholder => "Country" %> <p>
  <%= f.text_field :code, :placeholder => "3-letter code" %> <p>
  <%= f.text_area :details, :placeholder => "Airport details" %> <p>
  <%= f.submit %>
<% end %>

模型具有正确的属性:

class Airport < ActiveRecord::Base
  attr_accessible :city, :code, :country, :details, :name
end

我听说在控制器中调用助手不是一个好习惯,但我不知道把它放在哪里以便在正确的时间调用它。

我仍在快速掌握 Rails,因此我们将不胜感激任何调试帮助!

【问题讨论】:

  • 使用固定的错字,得到这个错误:JSON::ParserError in AirportsController#create 757: 'status: 404 code: NOT_FOUND message: Resource not found. '

标签: ruby-on-rails ruby helper


【解决方案1】:

您的 create_location 方法中有错字,使用 'aiports' 而不是 'airports'

【讨论】:

  • 哇,白痴。但遗憾的是,这不是问题。 name 字段仍然为零。
  • 我更新了问题以删除错字。不过谢谢!
  • 更改该错字不应该影响之前的代码,除非您现在走得更远并且在随后的呼叫中传递了错误的机场代码而失败。无论如何,您需要在解析结果正文之前检查结果状态,因为get 显然失败了。
【解决方案2】:

想通了!

事实证明,辅助方法工作得很好。因此,任何寻找帮助模块问题的人,这可能是一个很好的参考,以了解工作模块的外观。

问题出在@PeterAlfvin 建议的 JSON 调用上。它没有获取正确的数据。

这是正确的辅助方法:

module AirportsHelper
  def create_location(airport_code)
    airport = Airport.find_by_code(airport_code)

    result = Typhoeus.get("https://api.flightstats.com/flex/airports/rest/v1/json/iata/#{airport_code}?appId=[APP ID]&appKey=[APP KEY]")
    result_hash = JSON.parse(result.body)

    result_hash['airports'].each do |airport|
      @airport_name = airport['name']
    end

    airport.update_attributes(name: @airport_name)
    airport.save

  end
end 

注意 API 获取请求中的字符串插值变化。

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 2011-10-02
    • 1970-01-01
    • 2011-02-05
    • 2010-12-10
    • 2011-09-19
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多