我可能忽略了一些东西,但这让我很难过....我有一个表格将显示一个调查(它实际上是在一个部分呈现的)并且我正在使用 :remote => true 传递给控制器(更新)通过和通过 jQuery 进行的 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
更新.js.erb....
$('#questionarea').html( "<%= escape_javascript render :partial => "current_question" %> ");
$('#hiddenparms').html( "<%= escape_javascript render :partial => "hiddenparms" %> ");
不确定这是否与它有关,但我有一个只有一条记录的嵌套资源,这是我的 form_for origintes 是否可以从 fields_for 中的问题级别增加或者我应该完全消除顶层并且只需在问题级别启动我的 forms_for 然后在类别控制器中创建一个新方法来处理这个问题会尝试,如果它有效将作为我的答案发布