Rails simple_form 两个模型

发布于 2024-11-07 00:54:19 字数 477 浏览 0 评论 0原文

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

谢谢

I'm starting to use simple_form for 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-11-14 00:54:19

使用 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 中必需)

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

  • 添加了使用 input_html 的示例

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

温柔戏命师 2024-11-14 00:54:19

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

有关详细信息,请参阅 https://github.com/reu/simple_nested_form

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文