Forum: Ruby on Rails select in *.rhtml

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.
William Z. (Guest)
on 2006-04-05 21:43
I'm trying to get a select box of categories to appear in my items views
after I created everything using generate scaffold, but I can't get the
categories to appear in my forms.

class ItemsController < ApplicationController
  def new
    @item = Item.new
    @category = Category.find_all
  end

...

class Item < ActiveRecord::Base
    belongs_to :category
end

class Category < ActiveRecord::Base
    has_many :items
end

I found some news postings that said the select tag in my _forms.rhtml
was 'object', 'attribute', and 'list_items', but I've tried several
combinations and can't get this right.

This is my ~/app/views/items/_form.rhtml

<p><label for="item_category_name">Category</label><br/>
<%= select 'item', 'name', 'category'  %></p>

I think I've got the above wrong, but now sure how.

Here's my database. Basically, all I want to do is assign a category to
each item.

CREATE TABLE categories (
    id              int unsigned NOT NULL auto_increment,
    name            varchar(32),
    description     varchar(256),
    PRIMARY KEY (id)
);

CREATE TABLE items (
    id              int unsigned NOT NULL auto_increment,
    name            varchar(32),
    category_id     int unsigned,
    PRIMARY KEY (id)
);
François B. (Guest)
on 2006-04-05 21:59
(Received via mailing list)
Hello Ender,

2006/4/5, Ender C. <removed_email_address@domain.invalid>:
> <p><label for="item_category_name">Category</label><br/>
> <%= select 'item', 'name', 'category'  %></p>

I suggest you take a look at collection_select
http://api.rubyonrails.com/classes/ActionView/Help...

collection_select uses objects whereas select requires a pre-mashed
array of arrays.

If you want to reach the API document, check:
http://api.rubyonrails.com/

Bye !
This topic is locked and can not be replied to.