Newbie needing help! How to read from database?


#1

I’ve read through all the articles online but most/all of it discusses
the use of Scaffolding as a quick solution for reading/writing to a
database.

I follow all that but now I’m trying to simply get a field from a table
in the database to display in my .rhtml files.

I’ve been trying to figure this out but to no avail. Someone please
help! Thanks.

Tim


#2

Sorry. To explain further, here’s what I tried to do:

This is the line of code in my index.rhtml:

<%= link_to_remote(“Click Here”, :update => ‘introduce’, :url => {
:action => :show_intro }, :complete => visual_effect(:blind_down,
‘introduce’) ) %>

It’s a link to simply display a block of text taken from the database.

In my Controller:

def show_intro
@intro = Content.introText
end

In my Model:

def self.introText
find(1)
end

Where 1 is the ID for the table row. There are 3 fields in my table: id,
title and description.

I’m trying to get the text from the ‘description’ to display above.


#3

So you should have a database table named “contents”.

Then in your controller:
def show_intro
@intro = Content.find(1)
end

In show_intro.rhtml:
<%= @intro.description %>

This assumes there is a column in your “contents” table called
“description”. And you can leave your model “Content” completly virgin.
You can test to see if it worked by just going to
http://domain.com/my_controller/show_intro” and you should see your
text from the database.

Although you may want to start out without the AJAX thing, until you
have the basics down. It can make things much more complicated.

Alternatively if you still wanted “Content.intro_text” (lowercase with
underscores for method name is the standard ruby convention) to work:
def Content < ActiveRecord::Base
def self.intro_text
find(1).description
end
end

Then in any .rhtml file:
<%= Content.intro_text %>

Remember that “Content.find(1)” returns an entire row as an ActiveRecord
object. To access a specific field, just call it as a method to return
its value:
@content = Content.find(1)
@content.description #=> displays the description for row with id=1

Tim wrote:

Sorry. To explain further, here’s what I tried to do:

This is the line of code in my index.rhtml:

<%= link_to_remote(“Click Here”, :update => ‘introduce’, :url => {
:action => :show_intro }, :complete => visual_effect(:blind_down,
‘introduce’) ) %>

It’s a link to simply display a block of text taken from the database.

In my Controller:

def show_intro
@intro = Content.introText
end

In my Model:

def self.introText
find(1)
end

Where 1 is the ID for the table row. There are 3 fields in my table: id,
title and description.

I’m trying to get the text from the ‘description’ to display above.


#4

Tim wrote:

Thanks Alex!

That was very helpful. It didn’t work at first but I took it from there
and figured it all out without sacrificing the use of the Ajax
blind_down effect.

Thanks again. Much appreciated.

Tim

Another thing you can do is to generate scaffolding for your models and
read the code it creates. It’ll have simple actions that do things like
add a record, delete a record, list and show a record.

ruby script/generate scaffold modelname

Replace “modelname” with the name of your model.

Jeff


#5

Thanks Alex!

That was very helpful. It didn’t work at first but I took it from there
and figured it all out without sacrificing the use of the Ajax
blind_down effect.

Thanks again. Much appreciated.

Tim