在POST表单中使用嵌套属性进行Will_paginate

时间:2013-07-31 09:40:23

标签: ruby-on-rails ruby-on-rails-3 associations nested-attributes will-paginate

我有两种模式:自我评估和分数,其中自我评估has_many :scoresaccepts_nested_attributes_for :scores。新的自我评估表可能有多达60个嵌套分数。我想要做的是对表单中的嵌套属性字段实现分页。

我试图实现它而没有运气。

控制器

def new
  @self_assessment = SelfAssessment.new
  @scores = @self_assessment.scores.paginate(:page => params[:page], :per_page => 10, :total_entries=> @self_assessment.scores.size)
end

视图

<%= form_for @self_assessment do |f| %>
    <h2>At home...</h2>
    <% Category.where(at_home: true).each do |category| %>
        <h3><%= category.title %></h3>
        <% Act.where(category_id: category.id).each do |act| %>
            <%= f.fields_for :scores, Score.new do |builder| %>
                <%= builder.hidden_field :self_assessment_id, value: @self_assessment.id %>
                <%= builder.select(:rank, scales_for_select(act), prompt: "-Select-") %>
                <%= act.name %><br>
            <% end %>
        <% end %>
    <% end %>

    <h2>At work...</h2>
    <% Category.where(at_work: true).each do |category| %>
        <h3><%= category.title %></h3>
        <% Act.where(category_id: category.id).each do |act| %>
            <%= f.fields_for :scores, Score.new do |builder| %>
                <%= builder.hidden_field :self_assessment_id, value: @self_assessment.id %>
                <%= builder.select(:rank, scales_for_select(act), prompt: "-Select-") %>
                <%= act.name %><br>
            <% end %>
        <% end %>
    <% end %>
    <%= will_paginate @scores, :params => { :method => :post } %>
    <%= f.submit "Submit" %>

1 个答案:

答案 0 :(得分:1)

我建议为分页尝试一些javascript解决方案。尝试使用数据表。

点击此链接:http://datatables.net/examples/api/form.html

希望它有所帮助。