views:

17

answers:

1

OK, assume that I have a post table, and a category table. This is the model look like:

class User < ActiveRecord::Base    
  acts_as_authentic  
  has_many :posts     

end

and, that is the post models:

class Post < ActiveRecord::Base
  belongs_to :user
end

And this is the new.html.erb from post:

<% form_for(@post) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label :views %><br />
    <%= f.text_field :views %>
  </p>            
  <p>
    <%= f.label :category_id %><br />
    <%= f.text_field :category_id %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

I want to change the category_id to become a option tag, also, I would like to loop back the option in the edit.html.erb, how can I implement it? Thank you.

+1  A: 

You can use the collection_select helper:

f.collection_select(:category_id , Category.all, :id, :name, {:prompt => true})

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M002303

Victor Hugo