我可能忽略了一些东西,但这让我感到困惑....我有一个表格会显示一个调查(它实际上是部分呈现)而我正在使用:remote =>是通过jQuery传递给控制器(更新)thru和ajax调用,当我通过js.erb渲染回视图时,我用下一个问题替换问题,并用下一个id的id以原始形式呈现(在隐藏字段中)但是在提交后第一个显示器没有完成工作会呈现下一个问题文本,但是id没有递增,下一个提交调用实际上是提交给第一个记录。是否有一个js命令会在erb中增加它。
我的代码如下所示
<%= form_for(@category, :remote => true) do |f| %>
<ul id='questionarea'>
<%=@current_question %>
</ul>
<div id = 'hidden_field_usr' >
<%=f.hidden_field :user_id, {:value => params[:id]} %>
</div>
<div id = 'hidden_field_question' >
<%=f.hidden_field :question_id, {:value => @current_question_id} %>
</div>
<%=f.fields_for @answer do |a| %>
<ul id= 'answerarea'>
<%= a.radio_button(:agree , 5) %>
<%= a.label(:agree_5, "I completely agree ") %>
<%= a.radio_button(:agree , 4) %>
<%= a.label(:agree_4, "I agree ") %>
<%= a.radio_button(:agree , 3) %>
<%= a.label(:agree_3, "I kind of agree ") %>
<%= a.radio_button(:agree , 2) %>
<%= a.label(:agree_2, "I disagree ") %>
<%= a.radio_button(:agree , 1) %>
<%= a.label(:agree_1, "I completly disagree ") %>
This statement is:
<%= a.radio_button(:importance , 3) %>
<%= a.label(:importance_3, "Incredibly important ") %>
<%= a.radio_button(:importance , 2) %>
<%= a.label(:importance_2, "Kind of important") %>
<%= a.radio_button(:importance , 3) %>
<%= a.label(:importance_1, "A good thing trade for")%>
</ul>
<ul>
<li id ='comments'>Comments:
<%= a.text_field :comments %>
</li>
</ul>
<ul>
<%end %>
<div id = "qsubmit" > <%= f.submit %> </div>
<%end %>
我的控制器看起来像......
def update
@formresults = params[:category]
@category = Category.find(params[:id])
@formresults = params[:category]
@question_id = @formresults[:question_id]
@user_id = @formresults[:user_id]
@answer_details = @formresults[:answer]
@answer=@category.questions.find(@question_id).answers.find_by_user_id(@user_id)
@answer.agree = @answer_details[:agree]
@answer.importance = @answer_details[:importance]
@answer.importance = @answer_details[:comments]
if @answer.save then
@counter = @question_id.to_i
@counter = @counter+1
@current_question_rec = @category.questions.find(@counter)
@current_question = @current_question_rec.question
@current_question_id = @counter
@answer = @category.questions.find(@current_question_id).answers.find_by_user_id(@user_id)
respond_to do |format|
format.html { redirect_to @category }
format.js { render }
end
end
end
... update.js.erb
$('#questionarea').html( "<%= escape_javascript render :partial => "current_question" %> ");
$('#hiddenparms').html( "<%= escape_javascript render :partial => "hiddenparms" %> ");
不确定这是否与它有关但我有一个只有一条记录的嵌套资源,这是我的form_for origintes是否可以从fields_for中的问题级别增加或者我应该消除顶部完全水平,然后在问题级别启动我的forms_for然后在类别控制器中创建一个新方法来处理这个尝试,如果它工作将发布作为我的答案
答案 0 :(得分:0)
我想我发现了这个错误,请你出示你的模特。
if @answer.save then
@counter = @question_id.to_i
@counter = @counter+1
!here=> @current_question_rec = @category.questions.find(@counter)
@current_question = @current_question_rec.question
@current_question_id = @counter
@answer = @category.questions.find(@current_question_id).answers.find_by_user_id(@user_id)
respond_to do |format|
format.html { redirect_to @category }
format.js { render }
end
end
我刚刚更新了 - 错误是: =&GT;你想跳到下一个问题,但你没有传递正确的参数。现在,我已经看到了你的问题.rb(模型)
尝试:
!here=> @current_question_rec = @category.questions.find_by_question_id(@counter)
如果您在github(免费)上托管代码并在此处发布链接,那将非常有用,我可能会自己尝试您的应用程序并找到答案。
答案 1 :(得分:0)
我打算将此标记为已回答,因为我怀疑form_for循环只生成1条记录,因为我使用类别对象作为我的循环我应该使用问题对象感谢Rubybrah您的信息这似乎是一个我无法看到森林中的树木问题