我使用嵌套表单为多个数据输入添加一行表单。 但是,使用 link_to 生成的表单会丢失所有样式。 查看生成的代码,所有样式都在那里,但是当我添加字段时,它不会呈现样式。 我尝试了被呈现为表行和 div 行( Bootstrap )的部分。最初生成的行看起来很完美,但添加的行都被揉成一团,没有与任何东西对齐。 谢谢!
在 application_helper 中
def link_to_add_fields(name, f, association, readonly)
new_object = f.object.send(association).klass.new
id=new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render("my partial form", f: builder, readonly: readonly)
end
link_to(name, "#", class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
在我的 new.html 中
<table class="table table-bordered">
<thead>
<tr>
<th>Customer</th>
<th>Description</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody>
<%= f.fields_for :my_association do |builder| %>
<%= render "my_partial", f: builder%>
<% end %>
<%= link_to_add_fields "添加新行", f, :my_association, :readonly=>false%>
在我的部分
<tr>
<td><%= f.collection_select(:customer_id, @customers, :id, :fullname, :include_blank=>'Select') %>
<%= f.hidden_field(:id)%></td>
<td> <%= f.text_field(:description) %></div>
<td> <%= f.text_area(:note, :size=> "25x1") %></td>
<td> <%= f.hidden_field :_destroy %><%= link_to "remove", "#", class: "remove_fields"%> </td>
</tr>
最佳答案
这似乎是一个纯粹的 HTML/CSS 问题。
它的 <thead>
而不是 <theader>
并将部分包装在 <tbody>
中
试试这个:
<table class="table table-bordered">
<thead>
<tr>
<th>Customer</th>
<th>Description</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody>
<%= f.fields_for :my_association do |builder| %>
<%= render "my_partial", f: builder%>
<% end %>
</tbody>
</table>
<div class="container-fluid">
<div class="row-fluid"><td colspan=6><%= link_to_add_fields "Add New Line", f, :my_association, :readonly=>false%></div></td>
</div>
关于css - Rails 样式在 link_to 嵌套表单中丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34782273/