0

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">, ...]> 
Community
  • 1
  • 1
  • is your `@people = Person.all` returning collection object ? check it in rails console – sa77 Nov 09 '16 at 05:56
  • @sa77 sorry for the delay, I had to go learn what Rails console was... the output was very interesting, I edited it into the post to preserve formatting but the short version is all the data is nil... not sure if the database is getting seeded wrong or if the query itself is somehow broken but I'm leaning towards the seeding... – bullish_programmer Nov 09 '16 at 06:47
  • There's your problem.. DB related.. Your rails codebase looks fine – sa77 Nov 09 '16 at 06:58
  • Show your database.yml file and also share your terminal – Aniket Tiwari Nov 09 '16 at 07:03

1 Answers1

0

Well you are on a right path attr_accessible has been removed from rails 4 and now it uses strong parameters .

class PeopleController < ApplicationController
    def index
        @people = Person.all
    end

    private
        def person_params

          params.require(:person).permit(:name, :email,:ID)
        end
end

Create some records from rails console

People.create(name: "anik",email: "abc@gmail.com",ID: 2)

You Don't have any records inserted in database that is why it is now showing any records.Firstly you need to insert by rails console Just Make sure that what datatype you have used for column while inserting records in database

Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61