2011-05-14 46 views
1

我開始爲rails應用程序使用simple_form,並且在轉換一些表單時,我遇到了一個有兩個模型的工作,它們是嵌入式表單。這可能與simple_form?rails simple_form兩個模型

<% simple_form_for :topic, :url => forum_topics_path do |t| %> 
<%= t.input :name, :label => 'Topic' %></p> 

<p>First Post:<br/></p> 
Title: <%= text_field :post, :title %> <--- this is where i start having problems 
Body: <%= text_area :post, :body %> 
<%= t.submit 'Save' %> 

感謝

回答

8

使用simple_fields_for

<%= simple_form_for :topic, :url => forum_topics_path do |topic_builder| %> 
    <%= topic_builder.input :name, :label => 'Topic' %> 
    <%= topic_builder.simple_fields_for :post do |post_builder| %> 
    <p>First Post:</p> 
    <%= post_builder.input :title, :input_html => { :size => 30 } %> 
    <%= post_builder.input :body, :as => :text, :input_html => { :rows => 20, :cols => 50, :class => 'resizable' } %> 
    <% end %> 
    <%= topic_builder.submit 'Save' %> 
<% end %> 

  • 注意,在<%= simple_form_for ...<%= simple_fields_for=符號(用Rails 3.x的要求)

  • 刪除「Title:」和「Body:」文本。使用爲輸入生成的標籤,並根據需要使用CSS對其位置進行樣式設置。使用input_html