formattastic - Accepts_nested_attributes_for 中值的子集

发布于 2024-10-07 22:33:24 字数 2303 浏览 0 评论 0原文

我有一个模型:

class Contact < ActiveRecord::Base
  has_many :phones
  accepts_nested_attributes_for :phones
end

我想构建 50 个用户可以添加的电话号码(可能已经有电话 1 或 5,但我总是希望有 50 个可用) 在我的控制器中:

while contact.phones.length < 50
  contact.phones.build({:phone_type_id => PhoneType['a_cool_type'].id})
end

在我看来,我想要 2 列电话#s,每列 25 行

 <%= semantic_form_for contact do |form| %>
   <table width=50%>
   <%= form.inputs :for => :phones[0..25] do |phone_form| %>            
      <td align="center"><%= phone_form.input :number, :label => false %></td>
       ....
   <% end %>
   </table>             
   <table width=50%>
   <%= form.inputs :for => :phones[25..49] do |phone_form| %>           
      <td align="center"><%= phone_form.input :number, :label => false %></td>
       ....
   <% end %>
   </table>
<%end %>

显然这行:

<%= form.inputs :for => :phones[25..49] do |phone_form| %>

不起作用,但它传达了我的意图(我希望)。我想更好地控制 formattastic 如何获取底层对象关联。

以下内容有效,但如果没有花哨的 css,我无法轻松完成两列。

<%= form.inputs :for => :phones do |phone_form| %>

有什么建议吗?

---------- 更新 ----

我能够以迂回的方式解决这个问题: 我建立了一个单独的电话号码列表,不是 contact.phones.build,而是 Phone.new(:contact_id => contact.id) 并将它们存储在名为 @new_phones 的列表中

然后我的表单如下所示:

<%= semantic_form_for @contact, :url => ..., do |f| %>
   <% @new_phones[0...25].each_with_index do |phone, i| %>
      <%= f.fields_for :phones, phone, :child_index => i do |phone_form| %>             
         <%= render "phone_fields", {:phone_form => phone_form, :phone => phone} %>
      <%end%>
    <% end %>
   ....
   <% @new_phones[25...50].each_with_index do |phone, i| %>
        <%= f.fields_for :phones, phone, :child_index => i+25 do |phone_form| %>                
        <%= render "phone_fields", {:phone_form => phone_form, :phone => phone} %>
    <%end%>
  <% end %>  
<%end%>

这允许我在页面的一个部分显示 25 部手机,在另一部分显示 25 部手机,并使用nested_attributes_for :phones 在表单提交时按预期工作。

I have a model:

class Contact < ActiveRecord::Base
  has_many :phones
  accepts_nested_attributes_for :phones
end

I want to build 50 phone #s that users can add (there may already be phones 1 or 5, but I always want 50 available)
In my controller:

while contact.phones.length < 50
  contact.phones.build({:phone_type_id => PhoneType['a_cool_type'].id})
end

In my view, I want to have 2 columns of phone #s 25 rows each

 <%= semantic_form_for contact do |form| %>
   <table width=50%>
   <%= form.inputs :for => :phones[0..25] do |phone_form| %>            
      <td align="center"><%= phone_form.input :number, :label => false %></td>
       ....
   <% end %>
   </table>             
   <table width=50%>
   <%= form.inputs :for => :phones[25..49] do |phone_form| %>           
      <td align="center"><%= phone_form.input :number, :label => false %></td>
       ....
   <% end %>
   </table>
<%end %>

Obviously the line:

<%= form.inputs :for => :phones[25..49] do |phone_form| %>

doesn't work, but it conveys my intention ( I hope). I want to have more control over how formtastic grabs the underlying object association.

The following works, but I can't do two columns easily without fancy css.

<%= form.inputs :for => :phones do |phone_form| %>

Any suggestions?

---------- Update ----

I was able to get around this in a roundabout way:
I built up a separate list of phone #s not as contact.phones.build, but Phone.new(:contact_id => contact.id) and store those in a list called @new_phones

Then my form looks like this:

<%= semantic_form_for @contact, :url => ..., do |f| %>
   <% @new_phones[0...25].each_with_index do |phone, i| %>
      <%= f.fields_for :phones, phone, :child_index => i do |phone_form| %>             
         <%= render "phone_fields", {:phone_form => phone_form, :phone => phone} %>
      <%end%>
    <% end %>
   ....
   <% @new_phones[25...50].each_with_index do |phone, i| %>
        <%= f.fields_for :phones, phone, :child_index => i+25 do |phone_form| %>                
        <%= render "phone_fields", {:phone_form => phone_form, :phone => phone} %>
    <%end%>
  <% end %>  
<%end%>

This allowed me to display 25 phones on one part of the page, and 25 on another, with nested_attributes_for :phones working as expected on form submit.

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

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

发布评论

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

评论(1

§普罗旺斯的薰衣草 2024-10-14 22:33:24

我一直无法让嵌套属性按我想要的方式工作,但这可能有助于解决您的问题。

型号:

class Contact < ActiveRecord::Base
  has_many :phones
  accepts_nested_attributes_for :phones
end

控制器:
请注意,我们循环了 @contract.phones.build 50 次,这会创建 50 个新实例。

class Contact < ApplicationController
  def new
    @contact = Contact.new
    25.times do
      @contact.phones.build
    end

  end
end

查看 new.html.erb :

...
<%= p.semantic_fields_for :phones do |ec| %>
  <%= ec.input :number %>
<% end %>
...

我确实尝试了几次拦截循环的尝试,遗憾的是没有明确的干净效果。

I've always had problems with getting nested attributes working as I want but this may help resolve your issue.

Model:

class Contact < ActiveRecord::Base
  has_many :phones
  accepts_nested_attributes_for :phones
end

Controller:
See we're looping @contract.phones.build 50 times, this creates 50 new instances.

class Contact < ApplicationController
  def new
    @contact = Contact.new
    25.times do
      @contact.phones.build
    end

  end
end

View new.html.erb :

...
<%= p.semantic_fields_for :phones do |ec| %>
  <%= ec.input :number %>
<% end %>
...

I did try a few attempts to intercept the loop, sadly with no definite clean avail.

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