I'm working on a flight tracking application in Ruby (1.9.3) on Rails (3.2.11). This application has a Flight
class, which has, among other things, a string column tail_number
to track the tail number of the airplane that performed the flight.
I'm attempting to create an index view for the tail number, showing a listing of the tail numbers and the count of each, and linking to a view which will show all Flights
which have that tail number. I have the show page working, but I'm having trouble making link_to
work on the index page.
The show view is handled by the flights
controller's show_tail
action, which I've set up in routes.rb:
match '/tails' => 'flights#index_tails'
match '/tails/:tail_number' => 'flights#show_tail'
The flight controller groups the flights by tail number and provides a count:
def index_tails
@flight_tail_numbers = Flight.where("tail_number IS NOT NULL").group("tail_number").count
end
On my tail number index page (/app/views/flights/index_tails.html.erb), I create a table of tail numbers and the count of each, intending to have the tail number link to the appropriate show view:
<h1>Tail Numbers</h1>
<table>
<% @flight_tail_numbers.each do |tail_number, count| %>
<tr>
<td><%= link_to(tail_number, tails_path(tail_number)) %></td>
<td><%= count %>
</tr>
<% end %>
</table>
However, instead of the link_to
creating a link to /tails/tail_number (for example, /tails/N909EV
) like I'm hoping, it instead creates a link to /tails.tail_number (/tails.N909EV
). This link clearly does not send me to the flights#show_tail that I'm wanting.
I've successfully created indexes for other models that are associated with Flight (Airports, Trips) and and index for Flight itself, and they all work by using, for example, the link_to trip.name, trip_path(trip)
syntax I'm trying to use for tail numbers. So this would be pretty trivial if Tail_Number were a model and I can treat it the same way, but it seems far too trivial to be a model versus just an attribute of a given flight.
I'm assuming the reason it's not working for me is that when I'm using trip_path(trip)
, trip
is an object whereas in tails_path(tail_number)
, tail_number
is a string. But I'm not sure exactly how I should link to it. I've tried solutions like the named routes in Rails link_to action with URL parameters, but I get the same undefined method
problem if I do that, so I reverted back to what's listed above.
How should I format my link_to to make this work?