将 Formtastic 的语义_字段_for 与 has_many 关联结合使用

发布于 2024-09-19 22:17:33 字数 1169 浏览 5 评论 0原文

我正在尝试使用 formattastic 创建嵌套表单。我已在下面包含了我的代码,但遇到了一些问题,我也在下面列出了这些问题。有什么建议吗?谢谢。

# Home model
class Home < ActiveRecord::Base
  has_many :home_members
  accepts_nested_attributes_for :home_members, :reject_if => :all_blank, :update_only => true, :allow_destroy => true
end


# Form builder in members/new.html.erb
<%= semantic_form_for @home, :url => home_members_path(@home), :html => { :method => :post }, :remote => true do |f| %>
  <%= f.inputs do %>
    <%= f.semantic_fields_for :home_members do |h| %>
    <%= h.input :name %>
    <%= h.input :email %>
    <%= h.input :birthday, :as => :string %>
  <% end %>
<% end %>

# members_controller's new method; @home is set in a before filter
def new
  2.times{ @home.home_members.build }
end
  1. 保存主页时会创建默认用户。如何让表单仅显示新创建的记录而不显示现有记录?

  2. 如果#1 不可能,我如何更新现有记录?我在accepts_nested_attributes_for 上设置了update_only,但仍然创建了一条新记录。

  3. 我在控制器操作中执行 2.times{ @home.home_members.build } 。当我打印 @home.home_members 的大小时,我得到了 3 (一个已经存在),正如预期的那样。为什么表单只显示 2 组输入,其中一组使用现有的 home_member 数据填充?

I am trying to create a nested form using formtastic. I've included my code below but am running into some problems that I've also listed below. Any suggestions? Thanks.

# Home model
class Home < ActiveRecord::Base
  has_many :home_members
  accepts_nested_attributes_for :home_members, :reject_if => :all_blank, :update_only => true, :allow_destroy => true
end


# Form builder in members/new.html.erb
<%= semantic_form_for @home, :url => home_members_path(@home), :html => { :method => :post }, :remote => true do |f| %>
  <%= f.inputs do %>
    <%= f.semantic_fields_for :home_members do |h| %>
    <%= h.input :name %>
    <%= h.input :email %>
    <%= h.input :birthday, :as => :string %>
  <% end %>
<% end %>

# members_controller's new method; @home is set in a before filter
def new
  2.times{ @home.home_members.build }
end
  1. A default user is created when a Home is saved. How do I have the form display only the newly created records and not the existing one?

  2. If #1 isn't possible, how do I make the existing record update? I have update_only set on the accepts_nested_attributes_for, but a new record is still created.

  3. I am doing 2.times{ @home.home_members.build } in the controller action. When I print the size of @home.home_members I get 3 (one already exists) as expected. Why is the form only displaying 2 sets of inputs, one being populated with the existing home_member data?

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

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

发布评论

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

评论(2

各空 2024-09-26 22:17:33

很好回答问题1)仅显示新创建的对象

# Form builder in members/new.html.erb
<%= semantic_form_for @home, :url => home_members_path(@home), :html => { :method => :post }, :remote => true do |f| %>
  <%= f.inputs do %>
    <%= f.semantic_fields_for :home_members do |h| %>
    <% if h.object.new_record? %>
      <%= h.input :name %>
      <%= h.input :email %>
      <%= h.input :birthday, :as => :string %>
    <% end %>
  <% end %>
<% end %>

well to answer question 1) show only the newly created objects

# Form builder in members/new.html.erb
<%= semantic_form_for @home, :url => home_members_path(@home), :html => { :method => :post }, :remote => true do |f| %>
  <%= f.inputs do %>
    <%= f.semantic_fields_for :home_members do |h| %>
    <% if h.object.new_record? %>
      <%= h.input :name %>
      <%= h.input :email %>
      <%= h.input :birthday, :as => :string %>
    <% end %>
  <% end %>
<% end %>
大姐,你呐 2024-09-26 22:17:33

我过去曾成功地使用过 cocoon。 https://github.com/nathanvda/formtastic-cocoon

I've used cocoon with success in the past. https://github.com/nathanvda/formtastic-cocoon

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