Skip to content

Instantly share code, notes, and snippets.

@sj26
Created November 30, 2012 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sj26/4173716 to your computer and use it in GitHub Desktop.
Save sj26/4173716 to your computer and use it in GitHub Desktop.

This is an example full of compromises.

You probably want another page displaying the form.

A partial would be better for tracking events.

search is not really the correct terminology here, maybe lookup_tracking_number or something, but redirecting to a show page with an id parameter would be better.

To clarify the paths where these files belong:

  • config/routes.rb
  • app/models/shipping.rb
  • app/models/tracking_event.rb
  • app/controllers/shippings_controller.rb
  • app/views/shippings/index.html.erb
  • app/views/shippings/search.html.erb
<h1>Shipments</h1>
<%= form_tag search_shipments_path, method: :get do %>
<p>Enter a <strong>tracking number</strong> to see tracking history:</p>
<p><%= text_field_tag :tracking_number %></p>
<p><%= submit_tag "Search" %></p>
<% end %>
My::Application.routes.draw do
# ...
resources :shipments, only: [:index] do
get :search
end
# ...
end
<h1>Shipment <%= @shipment.tracking_number %></h1>
<p>Some shipping details here?</p>
<h2>Tracking Events</h2>
<ol>
<% @shipment.tracking_events.each do |tracking_event| -%>
<li><%= tracking_event.description %><%= time_ago_in_words tracking_event.created_at %></li>
<% end -%>
</ol>
class Shipment < ActiveRecord::Base
has_many :tracking_events
end
class ShipmentsController
def index
end
def search
@shipment = Shipment.includes(:tracking_events).find_by_tracking_number(params[:tracking_number])
end
end
class TrackingEvent < ActiveRecord::Base
belongs_to :shipment
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment