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

**URL:** <https://www.ruby-forum.com/t/how-do-i-list-strings-vertically-down-a-website-from-a-datab/111683>\
**Category:** Rails\
**Created:** [August 9, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/how-do-i-list-strings-vertically-down-a-website-from-a-datab/111683 "2007-08-09T19:53:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lodestone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@lodestone](https://www.ruby-forum.com/u/lodestone)\
**Post date:** [August 9, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/how-do-i-list-strings-vertically-down-a-website-from-a-datab/111683/1 "2007-08-09T19:53:49Z")

</div>

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\<\<

---

<div class="post-metadata">

**Author:** ![lodestone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@lodestone](https://www.ruby-forum.com/u/lodestone)\
**Post date:** [August 10, 2007, 1:57am UTC](https://www.ruby-forum.com/t/how-do-i-list-strings-vertically-down-a-website-from-a-datab/111683/2 "2007-08-10T01:57:19Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lodestone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@lodestone](https://www.ruby-forum.com/u/lodestone)\
**Post date:** [August 10, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/how-do-i-list-strings-vertically-down-a-website-from-a-datab/111683/3 "2007-08-10T17:13:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lodestone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@lodestone](https://www.ruby-forum.com/u/lodestone)\
**Post date:** [August 10, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/how-do-i-list-strings-vertically-down-a-website-from-a-datab/111683/4 "2007-08-10T23:48:40Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lodestone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@lodestone](https://www.ruby-forum.com/u/lodestone)\
**Post date:** [August 13, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/how-do-i-list-strings-vertically-down-a-website-from-a-datab/111683/5 "2007-08-13T16:26:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lodestone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e495f1/32.png) [@lodestone](https://www.ruby-forum.com/u/lodestone)\
**Post date:** [August 13, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/how-do-i-list-strings-vertically-down-a-website-from-a-datab/111683/6 "2007-08-13T17:04:31Z")

</div>

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