How do i list strings vertically down a website from a datab

hi,

I need to set up a page that lists words vertically, these words will be
read from a database. I would also like a text box at the bottom of the
list ready to add new words to the database, then have the newly added
word appear at the end of the list.

I own the agile web dev with rails book, but I cant get a pure answer to
my question from it. If you could just give me some lines of code you
think would work that would be great.

this is an example:

hello
whats up
hi

text box<< “how are you” (‘how are you’ is entered into the text box)

(the site will then reload and come back with this)

hello
whats up
hi
how are you

text box<<

This would probably do what you want… It would be more fun done
ajax calls (no page refresh) but this gives you the starting idea

—Controller
class SimpleListController < ApplicationController
def index
@items = SimpleList.find(:all, :order=>“id”)
end
def create_item
unless params[:display_text].empty?
@items = SimpleList.create(:display_text=>params[:display_text])
end
redirect_to :action=>:index
end
end

---- View
<% for item in @items -%>
<%=item.display_text %>

<% end -%>
<% form_tag :action=>:create_item do -%>
<%=text_field_tag :display_text %>
<%=submit_tag “Add” %>
<% end %>

On Aug 10, 3:53 am, Matt P. [email protected]

Mr. Bris,

Thank you for this code, this is definitely what I needed. There are a
couple problems that I still need your help on; when I ran the code on
localhost it came back with an error:

Showing app/views/sample_list/add.rhtml where line #5 raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #5):

2:
3: please enter a word…
4:
5: <% for item in @items -%>
6: <%=item.display_text %>

7: <% end -%>
8: <% form_tag :action=>:create_item do -%>

It talks about an unexpected nil character, so I’m thinking that I
should load the database with the first entry. If you think that is the
case, should I do some type of migration to make a new table in the DB?
In any event I would like to get your feed back.

Thanks again,

Matt

This should be the code in your migration file after you generated a
model…

class CreateSimpleLists < ActiveRecord::Migration
def self.up
create_table :simple_lists do |t|
t.column :display_text, :string
end
end

def self.down
drop_table :simple_lists
end
end

then “rake db:migrate”

On Aug 11, 1:13 am, Matt P. [email protected]

Mr. Bris

I did the migration, it showed up in the database but I still get the
same error. I tried putting something manually into the database, to
have a string in there for the code to grab, but it would not let me. Is
there anything else I’m not doing or seeing?

Thanks

Matt

to load the db, I made an admin controller and put scaffold :sample in
it, but the same error keeps coming up.