Ruby on rails 将使用嵌套属性在表单内部分页
我正在对调查问题使用will_paginate分页 每个问题都有一个答案 每页包含5个问题。在进入下一页时,我丢失了上一页的答案。如何在分页之前保存这些记录Ruby on rails 将使用嵌套属性在表单内部分页,ruby-on-rails,pagination,save,will-paginate,Ruby On Rails,Pagination,Save,Will Paginate,我正在对调查问题使用will_paginate分页 每个问题都有一个答案 每页包含5个问题。在进入下一页时,我丢失了上一页的答案。如何在分页之前保存这些记录 <%= form_for(@attempt, :url => attempt_scope(@attempt)) do |f| %> <%= f.fields_for :answers do |builder| %> <div id="accordion" class="questions"
<%= form_for(@attempt, :url => attempt_scope(@attempt)) do |f| %>
<%= f.fields_for :answers do |builder| %>
<div id="accordion" class="questions">
<% @questions.each_with_index do |question, index| %>
<h3 data-id="<%= question.id %>" >Question </h3>
<div>
<p> <div><%= question.text %> </div>
<%= hidden_field_tag "survey_attempt[answers_attributes][#{index}][question_id]", question.id %>
</p>
<div class="input select rating-c">
<label for="example-c">Rating values displayed on the bars:</label>
<%#=select_tag "survey_attempt[answers_attributes][#{index}][option_id][]", options_from_collection_for_select(@options, "id", "text"),{ class: " #{question.id}list select mybutton",id: "example-c",include_blank: true, name: "optionname"} %>
<%=select_tag "survey_attempt[answers_attributes][#{index}][option_id][]", options_from_collection_for_select(@options, "id", "text"),{id:"example-c"} %>
</div>
</div>
<% end %>
</div>
<%= will_paginate @questions, :class=>"mybutton page pagination", renderer: BootstrapPagination::Rails, params: request.params %>
<% end %>
<%= f.submit "Submit" %>
trunt_范围(@trunt))do | f |%>
问题:
条形图上显示的额定值:
“mybutton页面分页”,呈现程序:BootstrapPagination::Rails,params:request.params%>
当用户检查或选择选项时,为什么不使用ajax提交答案呢。您可以获得有关ajax的更多信息
编辑
有两种方法可以实现ajax,一种是使用remote:在我之前发布的链接中解释的表单中为true,另一种是通过jquery调用ajax请求。我认为jquery one在您的情况下会更干净
假设您有一个问题答案的复选框。您将有如下内容:
<input type="checkbox" class="answers"> #but each question will have different answers so you need something to differentiate so you will need some data attribute.
<input type="checkbox" class="answers" data-question="question_id">
这样,无论用户勾选哪个复选框,它都将调用相同的方法,您不必重复代码。可能重复的代码我没有得到任何正确的答案。阅读此页,我会这样做。您需要将每个复选框包装在
remote\u form\u中,以便
can i override will\u paginate controller实现我的要求。我可以从错误条件中逃脱ajax@santosh你为什么要把图书馆换成这么小的东西?如果使用ajax,效果会更好。您只需在controller中创建自定义方法,然后在每次用户勾选其答案时调用该方法。您可以发布一些示例,说明如何通过Ajax进行保存!
$(document).on("click",".answers",function(){
var question = $(this).data("question"); #gets the id of question
$.ajax({
url: url_for_your_custom_action, #need to make a method in controller and create answer for that question by the id of question you get from content in params
type: "POST", #a post request because you sending question id.
data: { content: question } #this will send your question id. you can get it by params[:content]
});
});