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


#1

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)


#2

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


#3

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


#4

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.