PanicBus
PanicBus

Reputation: 576

Controller not accessing helper method

I don't think my helper method is being accessed when trying to saving to my database. A new instance of Airport is being created but the data I'm expecting from the API is not there. It should bring in the airport's name based on its IATA code the user entered in a form_for in the view.

In other words, "name" is always nil in my db. Therefore doesn't seem like the API is being tapped at all, and name is never sent to the controller to save, which leads me to believe there is no call to the helper for some reason.

If it is actually being called, why is "name" not being filled?

This is my controller:

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

And here is my helper file:

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

This is my form (built in a partial):

<%= 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 %>

The model has the correct attributes:

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

I've heard it isn't good practice to call a helper in a controller but I don't know where to put it so that it's called at the right time.

I'm still getting up to speed with Rails so any debugging help would be appreciated!

Upvotes: 0

Views: 214

Answers (2)

PanicBus
PanicBus

Reputation: 576

Figured it out!

Turns out the helper method was working just fine. So anyone looking for problems with their helper modules, this may be a good reference on what a working one looks like.

The problem was with the JSON call as @PeterAlfvin suggested. It was not taking the correct data.

Here's the correct helper method:

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 

Note the string interpolation change in the API get request.

Upvotes: 0

Peter Alfvin
Peter Alfvin

Reputation: 29439

You've got a typo in your create_location method, with 'aiports' instead of 'airports'.

Upvotes: 1

Related Questions