我有两个模型:自我评估和评分,其中自我评估has_many :scores
和accepts_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" %>