2013-05-04 105 views
0

我可能忽略了一些东西,但这让我难倒....我有一个表格,它将显示一个调查(它实际上是呈现在一个部分),我正在使用:remote => true to通过jQuery传递给控制器​​(更新)thru和ajax调用,当我通过js.erb渲染回视图时,我用下一个问题替换问题,用下一个id替换原始窗体中的id(in隐藏的字段),但那不是做的工作提交后的第一个显示呈现下一个问题的文本,但ID不增加,下一个提交调用实际上是提交到第一个记录。是否有一个js命令会在erb中增加这个值。for_form增量问题增量对象

我的代码如下所示以下

<%= form_for(@category, :remote => true) do |f| %> 
<ul id='questionarea'> 
<%[email protected]_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] 

@[email protected](@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起源是poss可以从fields_for中的问题级别增加,或者我应该完全删除最高级别,然后在问题级别启动我的forms_for,然后在类别控制器中创建一个新方法来处理这个问题,如果它能够工作,将会发布为我的回答

回答

0

我打算把这个标记为答案,因为我怀疑form_for循环只会生成1条记录,因为我使用的是类对象作为我的循环我应该使用问题对象感谢Rubybrah为您的信息,这似乎是一个不能见树不见问题森林对我而言

0

你有你的应用程序/代码在github上?如果你分享链接,那么我可能会研究这个问题。

我想我发现了这个错误,可以请您出示您的模型。

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 

我刚刚更新了此 - 错误是: =>你想跳到下一个问题,但你是不是在传递正确的参数。现在,我已经看到了你question.rb(模型)

尝试:

!here=> @current_question_rec = @category.questions.find_by_question_id(@counter) 

,如果你在github上(免费)托管代码这将是非常有用的,在这里发布的链接,我可能亲自尝试您的应用程序并找出答案。

+0

我的类别类类别<的ActiveRecord :: Base的 attr_accessible:ID,:名称,:PIC,:small_pic,:question_attributes \t的has_many:问题,:依赖=>:destroy validates_presence_of:name,:id accepting_attributes_for:questions end – Jan 2013-05-04 20:42:22

+0

好的,现在请写下您的问题.rb(model) – rubybrah 2013-05-04 20:54:58

+0

我的问题。RB 类问题<的ActiveRecord :: Base的 \t attr_accessible:CATEGORY_ID,:问题:question_id,:answers_attributes belongs_to的:类 的has_many:回答 accepts_nested_attributes_for:回答 结束 – Jan 2013-05-04 20:55:45