# Can scaffold generate listboxes in views? I've been unsucces

**URL:** <https://www.ruby-forum.com/t/can-scaffold-generate-listboxes-in-views-ive-been-unsucces/50544>\
**Category:** Rails\
**Created:** [December 17, 2005, 11:22pm UTC](https://www.ruby-forum.com/t/can-scaffold-generate-listboxes-in-views-ive-been-unsucces/50544 "2005-12-17T23:22:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Self](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@Jeff\_Self](https://www.ruby-forum.com/u/Jeff_Self)\
**Post date:** [December 17, 2005, 11:22pm UTC](https://www.ruby-forum.com/t/can-scaffold-generate-listboxes-in-views-ive-been-unsucces/50544/1 "2005-12-17T23:22:25Z")

</div>

I’ve created several different Rails applications but I’m running  
into the same trouble with each one. Lets say I create an app called  
Test. I type ‘rails test’ and it generates the directory for me.  
Now I edit my database.yml file. Then I create a database with  
Postgresql by typing createdb test. Now I go into Postgresql by  
typing ‘psql test’.

Here are my tables:

CREATE TABLE topics (  
id SERIAL PRIMARY KEY,  
name VARCHAR(100) NOT NULL  
);

CREATE TABLE blogs (  
id SERIAL PRIMARY KEY,  
title VARCHAR(100) NOT NULL,  
topic\_id INTEGER NOT NULL REFERENCES topics(id),  
content TEXT NOT NULL  
);

Next, back in the test folder, I type ‘script/generate scaffold Topic  
Topic’. This creates my topic pages. So far, so good. Next, I type  
‘script/generate scaffold Blog Blog’. This creates my blog pages.  
When I go to view the blog pages at localhost:3000/blog, the only  
fields showing up on my page are the title and content. Why isn’t  
the topic\_id list box displayed?

I know I’ve gotten it to work once or twice but I don’t think I’m  
leaving out any steps, am I?

–  
Jeff Self  
Mac OS X (Apple Styling, Unix Power)

---

<div class="post-metadata">

**Author:** ![Jeff\_Self](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@Jeff\_Self](https://www.ruby-forum.com/u/Jeff_Self)\
**Post date:** [December 17, 2005, 11:49pm UTC](https://www.ruby-forum.com/t/can-scaffold-generate-listboxes-in-views-ive-been-unsucces/50544/2 "2005-12-17T23:49:37Z")

</div>

On Dec 17, 2005, at 2:21 PM, Jeff Self wrote:

> id SERIAL PRIMARY KEY,  
> Next, back in the test folder, I type 'script/generate scaffold
> 
> Jeff Self  
> Mac OS X (Apple Styling, Unix Power)

Jeff-

```
Scaffolding is not smart enough to handle relationships like that.

```

You have to set that part up manually.

Cheers-  
-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![Jeff\_Self](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@Jeff\_Self](https://www.ruby-forum.com/u/Jeff_Self)\
**Post date:** [December 18, 2005, 11:39am UTC](https://www.ruby-forum.com/t/can-scaffold-generate-listboxes-in-views-ive-been-unsucces/50544/3 "2005-12-18T11:39:20Z")

</div>

On Sunday 18 December 2005 05:54, Nic W. wrote:  
…

> ```
> </option>
> 
> ```
> 
> \<% end %\>
> 
> I don’t know really where you’re stuck, some little more direction  
> would better. This is to get you started, but is definitely not the  
> DRY way.
> 
> - Nic.

A slightly shorter way to do:

\<%= select :blog, :topic\_id, Topics.find(:all).map{ |t| [t.name, t.id]}  
%\>

quite violating the MVC architecture, so a cleaner way would be to  
compute the  
Topics list in the controller as you did.

Francois

---

<div class="post-metadata">

**Author:** ![Jeff\_Self](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@Jeff\_Self](https://www.ruby-forum.com/u/Jeff_Self)\
**Post date:** [December 17, 2005, 11:55pm UTC](https://www.ruby-forum.com/t/can-scaffold-generate-listboxes-in-views-ive-been-unsucces/50544/4 "2005-12-17T23:55:39Z")

</div>

I’m still new, and my syntax is probably off, but it is happening  
because of your foreign key reference. Don’t forget to set your  
assocations in the model, the ‘has\_many’ and ‘belongs\_to’. Once this  
is done, you can access the value by: blog.topic:

For example, just to make the drop-down appear under ‘Show’ when you  
click on a particular blog (this should be done w/partials and such  
really)

Edit your Controller for blogs, and add a line like this under Show:  
@topics= Topics.find\_all

Test it out by editing show.rhtml for blogs and adding a line like this:

Topic

 \<% @topics.each do |topic| %\> \> \<%= topic.name %\> \<% end %\> 

I don’t know really where you’re stuck, some little more direction  
would better. This is to get you started, but is definitely not the  
DRY way.

- Nic.
