I'm brand new to Ruby on Rails. Trying to create a starter project modeled after the simple "Rotten Potatoes" sample app from the popular MOOC textbook Engineering Software as a Service. The sample code from the book works fine (duh), but when I try to make some changes I get some weird behavior. I am using Rails 5.0.0.1 which is not what the book uses!
I'm trying to do a simple database read and display the results in a table. The page I generate has the right number of rows in the table so I know there is some successful database connection. Even if I add more sample data to the database and reload the app, the page shows the new correct number of table rows. But the error is, it does not populate the table cells with any data.
I'm using MVC. My code:
Model, person.rb
:
class Person < ActiveRecord::Base
# attr_accessor :name, :email, :ID
# The above line is equally as broken as the current state
# attr_accessible :name, :email, :ID
# Something like this is what was used in the original Rotten Potatoes
# Cannot use attr_accessible in Rails 5... must use strong parameters instead?
# https://github.com/rails/strong_parameters
end
Controller, people_controller.rb
:
class PeopleController < ApplicationController
def index
@people = Person.all
end
private
def person_params
params.require(:person).permit(:name, :email, :id)
end
end
View, index.html.haml
:
%table#people
%thead
%tr
%th Name
%th E-mail address
%th ID
%th Edit
%tbody
- @people.each do |person|
%tr
%td= person.name
%td= person.email
%td= person.ID
%td= link_to "Edit", edit_person_path(person)
Actual resulting generated HTML:
<table id='people'>
<thead>
<tr>
<th>Name</th>
<th>E-mail</th>
<th>ID</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td><a href="/people/1/edit">Edit</a></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><a href="/people/2/edit">Edit</a></td>
</tr>
...
Desired generated HTML:
<table id='people'>
<thead>
<tr>
<th>Name</th>
<th>E-mail</th>
<th>ID</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Q. Tester</td>
<td>jqt@example.com</td>
<td>0001</td>
<td><a href="/people/1/edit">Edit</a></td>
</tr>
<tr>
<td>Testy D. Smith</td>
<td>tds@example.org</td>
<td>0002</td>
<td><a href="/people/2/edit">Edit</a></td>
</tr>
...
At first I was using attr_accessible
in the model. But I got an error and the message suggested trying attr_accessor
instead. So I changed it and the app ran without errors but the data did not show up in the table. Then I found out that Rails 4 stopped supporting attr_accessible
and it was replaced by strong parameters. So I tried using those but it did not improve results. I don't know if I tried the wrong thing or if I am on the right path and just did it wrong.
Sorry if this isn't enough information. Please just tell me and I will do my best to update. At this point I dont know what is even relevant for an MCVE for Ruby on Rails.
EDIT:
Rails console output:
2.3.0 :005 > @people = Person.all
Person Load (0.3ms) SELECT "people".* FROM "people"
=> #<ActiveRecord::Relation [#<Person id: 1, name: nil, ID: nil, email: nil, created_at: "2016-11-08 08:14:17", updated_at: "2016-11-08 08:14:17">, #<Person id: 2, name: nil, ID: nil, email: nil, created_at: "2016-11-08 08:14:17", updated_at: "2016-11-08 08:14:17">, ...]>