2013-05-05 79 views
1

我想渲染一個局部在我的頁面在控制檯或終端日誌中沒有錯誤場景是......用戶選擇頁面上的鏈接和中間區域應該呈現。我正在運行這個通過jquery並讓提交去我的控制器'會議'和方法'showsurvey'它獲得必要的信息,然後呈現到showurvey.js.erb用showurvey部分渲染局部不顯示控制檯中沒有錯誤

這裏是所涉及的代碼.... shownewuser.html.erb

 <%= include_gon %> 


<div id="content" class="clearfix"> 
<%= render :partial => 'layouts/user_profile' %> 
     <div class="inner"> 

     <div class="top_area">   
      </div> <!-- top_area -->    
      <p>Welcome <%[email protected]%> Drag the topics into the order of importance to you.</p> 
      <div id="middle_area" > 
      <%= render :partial => 'middle_newuser' %> 
     <div id="find"> 
     <p>Where voters and politicians can discuss and learn about the issues for free with no advertising dollars getting in the way <br /> 
     Subscribe to RSS feeds to stay in touch with their latest votes. </p> 
     </div> 

</div> <!-- inner_area --> 

原單局部更換_mi ddle_newuser.html.erb

   <div id = "draggable" class="ui-widget-content"> 
     <h3 class="ui-widget-header">Issues</h3> 
     <table> 
     <tr><td> 
     <ul id ="column1" > 
     <% (0..5).each do |i| %> 
     <% @sm_pic= @cat[i][:small_pic] 
      @CatName = @cat[i][:name] 
      @id = @cat[i][:id]%> 
       <li id="draggable"> 
      <%= link_to image_tag(@sm_pic, :border => 0), showsurvey_path({:data => @id}), :remote => true, :data => @id , :id => 'showSurvey' %> 
      </li> 
     <% end %> 
     </ul> 
      </td> 
        <td> 
      <ul id ="column1" > 
      <% (6..11).each do |i| %> 
      <% @sm_pic= @cat[i][:small_pic] 
      @CatName = @cat[i][:name] 
      @id = @cat[i][:id]%> 
       <li id="draggable"> 
      <%= link_to image_tag(@sm_pic, :border => 0), showsurvey_path, :data => @id %> 
         </li> 
       <% end %> 
     </ul> 
     </td> 
      <td> 
     <ul id ="column1" > 
     <% (12..17).each do |i| %> 
     <% @sm_pic= @cat[i][:small_pic] 
      @CatName = @cat[i][:name] 
      @id = @cat[i][:id]%> 
       <li id="draggable"> 
      <%= link_to image_tag(@sm_pic, :border => 0), showsurvey_path, :data => @id %> 
       </li> 

     <% end %> 
      </ul> 
      </td></tr> 
     </table> 
     </div id> <!-- draggable --> 
      </div> <!-- middle_area --> 

和應該替換此...._ middle_survey.html.erb

   <div> 
     <table><tr> 
      <div id="middle_area-left" > 
      <p>Welcome this will be the survey page for user after clicking on a priority icon.</p> 
      </div> 
      <div id="middle_area-center" > 
      <%= render :partial => 'form' %> 
      </div> 
      <div id="middle_area-right" > 
      <p>Welcome this will be the survey page for user after clicking on a priority icon.</p> 
      </div> 
      </tr> 
      </table> 
      </div> 

控制器方法的部分是showSurvey

def showsurvey 
    @title = "Survey for" 
    @category = Category.find(params[:data]) 
     @questions = @category.questions.find_all_by_category_id(params[:data]) 
     @current_question_rec = @questions.first 
     @current_question = @current_question_rec.question 
     @current_question_id = @current_question_rec.question_id 
     @answer = @category.questions.find(@current_question_id).answers.find_by_user_id(params[:id]) 
    respond_to do |format| 
    format.html { redirect_to @category } 
     format.js { render } 
end   

end

last showSurvey.js.erb

這裏
$(#middle_area').replace_html("<%= escape_javascript render :partial => "middle_survey" %> "); 

一件事是從我的終端輸出

Processing by SessionsController#showsurvey as JS 
    Parameters: {"id"=>"3", "data"=>"3"} 
    Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", "3"]] 
    Question Load (0.4ms) SELECT "questions".* FROM "questions" WHERE "questions"."category_id" = 3 AND "questions"."category_id" = '3' 
    Question Load (0.3ms) SELECT "questions".* FROM "questions" WHERE "questions"."category_id" = 3 AND "questions"."question_id" = ? LIMIT 1 [["question_id", 1]] 
    Answer Load (0.3ms) SELECT "answers".* FROM "answers" WHERE "answers"."question_id" = 1 AND "answers"."user_id" = 3 LIMIT 1 
    Rendered sessions/_form.html.erb (4.1ms) 
    Rendered sessions/_middle_survey.html.erb (5.8ms) 
    Rendered sessions/showsurvey.html.erb (7.6ms) 
Completed 200 OK in 26ms (Views: 13.9ms | ActiveRecord: 1.1ms) 

感謝您的任何援助

回答

0
$(#middle_area').replace_html("<%= escape_javascript render :partial => "middle_survey" %> "); 

看起來像一個錯字,你忘了前進的報價。

$( #middle_area」)

而且,我不是在CSS那麼好,但我看不出格#middle_area本身,你肯定jQuery可解析#middle_area-right等等?

+0

啊我看你說什麼,但他們是實際有效的CSS元素我會認爲它會樹下這個,但我不知道我的自我。我確實改變了js謝謝,還發現了一些其他的拼寫錯誤,但仍然沒有去。 – Jan 2013-05-05 23:27:48

+0

如果發現元素,您可以進行快速測試併發出警報 – 2013-05-05 23:29:48

+0

另外,使用format.js {render}去除括號,因爲您只是傳遞nil。我檢查了你的鏈接,我敢肯定,爲什麼rails甚至不解析js文件,是因爲你的語法違反了link_to的規則。檢查你鏈接在brouser中,是不是通過:remote => true作爲params?有沒有nofollow標籤? – 2013-05-05 23:36:53

0

發現問題所有美妙的jquery返回像「replace_html」等等不起作用,因爲當你從控制器呈現js時,它認爲它是普通的舊js,因此它返回到原型對象(深埋在jquery中)其實,我發現這個通過讀取另一個計算器問題回答「使用jQuery護欄更換原型 - replace_html導致錯誤」

底線是你真正需要的在線調試和走通了jQuery看到創建錯誤:

在我的情況下,它存在於一個函數 jquery叫做ajaxConvert(s,response)

......有一次我在控制檯中輸入的對象

conv(response) it displayed the following ..... 

類型錯誤:對象的翻譯:對渲染軌道 通過搜索,我發現了計算器問題的答案沒有方法「replace_html」。 再一次受火洗!