【问题标题】:rails simple_form two modelsrails simple_form 两个模型
【发布时间】:2012-11-06 09:07:00
【问题描述】:

我开始将 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' %>

谢谢

【问题讨论】:

    标签: ruby-on-rails simple-form


    【解决方案1】:

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

    注意事项

    • 注意&lt;%= simple_form_for ...&lt;%= simple_fields_for 中的= 符号(Rails 3.x 中需要)

    • 删除了“标题:”和“正文:”文本。使用为输入生成的标签,并根据需要使用 CSS 设置其位置。

    • 添加了使用input_html的示例

    【讨论】:

      【解决方案2】:

      我正在使用另一种方法,效果很好。 Ryan Bates (RailsCasts) 创建了一个 gem 来处理这个问题。

      详情请见https://github.com/reu/simple_nested_form

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多