开发者

rails simple_form two models

I'm starting to use simple_form for 开发者_StackOverflow社区a rails application, and while converting some of my forms, I came across one that has two models that it is working with, sort of an embedded form. Is this possible with 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' %>

Thanks


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

Notes

  • Note the = symbol in <%= simple_form_for ... and <%= simple_fields_for (required in Rails 3.x)

  • Removed "Title:" and "Body:" text. Use the label generated for the inputs and style their location with CSS as needed.

  • Added example of using input_html


There's another approach that I'm using and it works great. Ryan Bates (RailsCasts) has created a gem to handle this.

See https://github.com/reu/simple_nested_form for the details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜