显示来自搜索查询的额外数据

时间:2018-06-20 11:08:30

标签: ruby-on-rails ruby api rubygems

目前正在通过Udemy课程进行学习,但是将讲座更改为我更感兴趣的课程。

项目依据: -使用Gem Flightstats-flex获取飞行数据 -尝试显示搜索查询中的关联信息

当前设置

app / controllers / trackers_controller.rb

class TrackersController < ApplicationController

  def search
    @tracker = FlightTracker.new_from_lookup(params[:tracker])
    render 'users/my_tracker'
  end

end

app / models / flight_tracker.rb

class FlightTracker < ActiveRecord::Base

    self.table_name='trackers'

    def self.new_from_lookup(tracker)
      flight_track = FlightStats::FlightStatus.by_flight_id(tracker)
      # flight_position = FlightStats::FlightStatus.track_by_flight_id(tracker)

end

当我从#移除flight_position = FlightStats::FlightStatus.track_by_flight_id(tracker)

时,就会出现问题
  • 结果覆盖顶部
  • 如果我仅使用显示的那行,反之亦然

app / views / users / my_tracker.html.erb数据确实呈现

<h1>
  My Tracker
</h1>

<h3>Search for Flights</h3>
<div id="tracker-lookup">
  <%= form_tag search_trackers_path, method: :get, id: "tracker-lookup-form" do %>
    <div class="form-group row no-padding text-center col-md-12">
      <div class="col-md-10">
        <%= text_field_tag :tracker, params[:tracker], 
                                  placeholder: "Stock ticker symbol", autofocus: true,
                                  class: "form-control search-box input-lg" %>
      </div>
      <div class="col-md-2">
        <%= button_tag(type: :submit, class: "btn btn-lg btn-success") do %>
          <i class="fa fa-search"></i> Look up a tracker
        <% end %>
      </div>
    </div>

  <% end %>
</div>

<div class="well results-block">
  <%=@tracker.inspect%>
    <strong>ID: </strong> <%#= @tracker&.flight_id %>
    <strong>Code: </strong> <%#= @tracker&.carrier_fs_code %>
    <strong>Number: </strong> <%#= @tracker&.flight_number %>
    <strong>Departure: </strong> <%#= @tracker&.departure_airport_fs_code %>
    <strong>Arrival: </strong> <%#= @tracker&.arrival_airport_fs_code %>
    <%# <strong>Departure Terminal: </strong> <%= @tracker&.departure_terminal %> %>
  <%=@position.inspect%>  
</div>

因此,当用户搜索.by_flight_id时,我希望它显示.track_by_flight_id的相关数据

希望在查询完成后进入更多数据关联的下一个阶段。

1 个答案:

答案 0 :(得分:0)

作为我的评论的例证

def self.new_from_lookup(tracker)
  flight_track = FlightStats::FlightStatus.by_flight_id(tracker)
  flight_position = FlightStats::FlightStatus.track_by_flight_id(tracker)
  OpenStruct.new(flight_track: flight_track, flight_position: flight_position)
end

然后,您需要重写模板 my_tracker.html.erb 来处理新的数据结构