Forum: Ruby on Rails Can scaffold generate listboxes in views? I've been unsucces

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
17bfc311c2da1e3954d493fab676864c?d=identicon&s=25 Jeff Self (Guest)
on 2005-12-17 23:22
(Received via mailing list)
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)
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-17 23:49
(Received via mailing list)
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 Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
B0e227ae419295321ca2aff4c27ad99d?d=identicon&s=25 Nic Werner (Guest)
on 2005-12-17 23:55
(Received via mailing list)
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:

<p>Topic</p><select name="blog[topic_id]">
   <% @topics.each do |topic| %>
       <option value="<%= topic.id %>"
         <%= ' selected' if topic.id == @device.topic_id %>>
         <%= topic.name %>
       </option>
   <% end %>
  </select></p>

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.
B7aa6f0356566cc801f769c6fc14ba1a?d=identicon&s=25 Francois GORET (Guest)
on 2005-12-18 11:39
(Received via mailing list)
On Sunday 18 December 2005 05:54, Nic Werner wrote:
....
>        </option>
>    <% end %>
>   </select></p>
>
> 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
This topic is locked and can not be replied to.