目前正在通过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
的相关数据
希望在查询完成后进入更多数据关联的下一个阶段。
答案 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 来处理新的数据结构