Jeff Self
2005-Dec-17 22:21 UTC
Can scaffold generate listboxes in views? I''ve been unsuccessful.
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)
Ezra Zygmuntowicz
2005-Dec-17 22:46 UTC
Re: Can scaffold generate listboxes in views? I''ve been unsuccessful.
On Dec 17, 2005, at 2:21 PM, Jeff Self wrote:> 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)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-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Nic Werner
2005-Dec-17 22:54 UTC
Re: Can scaffold generate listboxes in views? I''ve been unsuccessful.
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. On 12/17/05, Jeff Self <jeff.self-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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) > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Francois GORET
2005-Dec-18 10:35 UTC
Re: Can scaffold generate listboxes in views? I''ve been unsuccessful.
On Sunday 18 December 2005 05:54, Nic Werner wrote: ....> 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. >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