noccor
noccor

Reputation: 3

Rails/Sqlite 3 - Displaying table data on page

I'm very very very new to Rails, and I've got a fast approaching deadline to code my first rail app as part of a challenge.

I've so far created the basic blog application, to get my head around how rails works.

I need to display a list of all data held within a table on a page in my site. In blog, I've got an index page - which lists all blog posts so I've been trying to follow the same process.

I've created a populated table in my database, and a controller - but when I try and display info from that database on my page I get the following error:

uninitialized constant RecordsController::Aliens

class RecordsController < ApplicationController
    def index
        @records= Records.all
    end
end

All I want to achieve is a simple table, which has the list of my records in, so far I'm not having much luck retrieving the data from the database.

Any help appreciated!

Upvotes: 0

Views: 296

Answers (1)

siegy22
siegy22

Reputation: 4413

I assume you want to list all aliens am I right?

As your model is called Alien you can call it like that:

def index
  @aliens = Alien.all
end

And then in your view something like:

<% @aliens.each do |alien| %>
  <div class="alien">
    <ul>
      <% alien.attributes.each do |attr_name, attr_value| %>
        <li><strong><%= attr_name %>:</strong> <%= attr_value %></li>
      <% end %>
    </ul>
  </div>
  <hr>
<% end %>

Upvotes: 2

Related Questions