在link_to嵌套表单上丢失了Rails样式

时间:2016-01-14 05:24:38

标签: css ruby-on-rails forms twitter-bootstrap nested

我使用嵌套表单为多个数据输入添加一行表单。 但是,使用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 %>

&lt;%= link_to_add_fields&#34;添加新行&#34;,f,:my_association,:readonly =&gt; false%&gt;     
    

在my_partial

<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>

2 个答案:

答案 0 :(得分:0)

这似乎是纯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>

答案 1 :(得分:0)

我不得不将我的部分包裹在自己的桌子中,取出我对部分呼叫的围绕行。这打破了桌面,但这是排队的唯一方式。

new.html.erb

<table class="table table-striped">
    <thead>
        <tr>
            <th>Customer</th>
            ...
            <th>Notes</th>
            <th>*</th>
        </tr>
    </thead>

<tbody>
    <%= f.fields_for :my_association  do |builder| %>

        <%= render "my_partial", f: builder%>
    <% end %>

<tr>
    <td colspan=6>
<%= link_to_add_fields "Add New Line", f, :my_association, :readonly=>false%>
</td></tr>
</tbody>
</table>

在我的paritial

<table class="table table-striped">
    <tbody>
    <tr style="width:100%">
            <td><%= f.collection_select(:customer_id, @customers, :id, :fullname, :include_blank=>'Select') %>

            ...

           <td> <%= f.text_area(:note, :size=> "25x1") %></td>
           <td> <%= f.hidden_field :_destroy %><%= link_to "remove", "#", class: "remove_fields"%> </td>

</tr>
</tbody>
</table>

我希望有一个更好的解决方案,但现在可以使用。