0

OK、投稿テーブルとカテゴリテーブルがあると仮定します。これは次のようなモデルです。

class User < ActiveRecord::Base    
  acts_as_authentic  
  has_many :posts     

end

そして、それはポストモデルです:

class Post < ActiveRecord::Base
  belongs_to :user
end

そしてこれは投稿からのnew.html.erbです:

<% 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 %>

category_idをオプションタグに変更したいのですが、edit.html.erbのオプションをループバックしたいのですが、どうすれば実装できますか?ありがとうございました。

4

1 に答える 1

1

collection_select ヘルパーを使用できます。

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

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

于 2010-07-12T13:56:16.793 に答える