State Selector From Collection or State Model

Posted by Chris Blackburn Thu, 19 Jun 2008 01:01:00 GMT

schema.rb for states table
create_table "states", :force => true do |t|
  t.string   "name"
  t.string   "abbreviation", :limit => 2
  t.datetime "created_at"
  t.datetime "updated_at"
end
cities_controller.rb
# GET /cities/new
# GET /cities/new.xml
def new
  @city = City.new
  @states = State.find(:all, :order => :abbreviation)

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @city }
  end
end
new.html.erb view for cities_controller.rb
<% form_for(@city) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :state_id %><br />
    <%= f.collection_select(:state_id, @states, "id", "abbreviation") %>
  </p>
  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

Posted in  | Tags , , , , , ,  | no comments | no trackbacks

Comments

Trackbacks

Use the following link to trackback from your own site:
http://blog.cbciweb.com/trackbacks?article_id=state-selector-from-collection-or-state-model&day=18&month=06&year=2008

(leave url/email »)

   Comment Markup Help Preview comment