Topic: Showing Driver Name instead of ID

I am learning Rails.  I have looked for a while and have two current challenges but I'm focusing on one here.  It took me some time to to figure out the foreign ID and to make the form work.  I originally thought I needed a nested model but clearly I only needed a foreign key (beginners confusion). 

I have two models/tables:

Trip with fields (:driver_id and :begin)
Driver (:name)

These two tables are associated as follows (Feel free to tell me if I'm modeling this incorrectly):

Trip has_many :drivers
Drivers belongs_to :Trip

I am using simple form for _form.html.erb here:

<%= simple_form_for(@trip) do |f| %>
  <%= f.error_notification %>
  <%= f.input :begin %>
  <%= f.association :drivers, :as => :check_boxes %>
<%= f.error :base %>
<%= f.button :submit %>
<% end %>

I want to show the driver name on the main index view in this file /views/trips/index.html.erb instead of the :id:

<% @trips.each do |trip| %>
  <tr>
 

<td><%= trip.driver_id %></td>

    <td><%= trip.begin %></td>
    <td><%= link_to 'Show', trip %></td>
    <td><%= link_to 'Edit', edit_trip_path(trip) %></td>
    <td><%= link_to 'Destroy', trip, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>

I would assume that instead of trip.driver_id I'd use drivers.name instead. 

Secondary issues:

When I'm creating a trip, selecting the driver with a checkbox, it does not save/show the actual driver on the index.

Last edited by veex (2012-06-29 11:10:57)

Re: Showing Driver Name instead of ID

veex wrote:

...
I would assume that instead of trip.driver_id I'd use drivers.name instead. 

...

That would be trip.driver.name

Re: Showing Driver Name instead of ID

I changed:
<td><%= trip.driver_id %></td>

to trip.drivers.id

This worked to display the drivers name.  However I still have the secondary issue.  It just displays "DRIVER" for all trips.  It's not displaying the actual name of the driver.

Re: Showing Driver Name instead of ID

The foreign key :drivers_id in the CREATE NEW TRIP is not being persisted to my trips table.  Therefore it doesn't know what driver is being dispatched to the trip.

Re: Showing Driver Name instead of ID

It just displays "DRIVER" for all trips.  It's not displaying the actual name of the driver.thank you!




-------------------------------------------------------------------------------------
Rc Hobby|rc helicopters reviews