1

我正在尝试在我的页面上呈现部分内容控制台或终端日志中没有错误,场景是......用户选择页面上的链接并且中间区域应该呈现。我正在通过 jquery 运行此程序,并让提交转到我的控制器“会话”和“showsurvey”方法,它获取必要的信息,然后呈现给 showsurvey.js.erb 以用 showsurvey 部分替换 html

这是涉及的代码....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 <%=@user.email%> 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 -->

替换 _middle_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

最后一个 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)

感谢您的帮助

4

2 回答 2

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

看起来像一个错字,你忘记了前引号。

$( ' #middle_area')

另外,我CSS不太好,但是我看不到div#middle_area本身,你确定jquery可以解析#middle_area-right等等吗?

于 2013-05-05T23:11:54.757 回答
0

发现问题所有精彩的 jquery 返回,如“replace_html”等都不起作用,因为当你从控制器渲染 js 时,它认为它是普通的旧 js,因此它返回到原型对象(深埋在 jquery 中)我实际上发现这通过阅读另一个 stackoverflow 问题来回答“用 jquery-rails 替换原型 - replace_html 会导致错误”

底线是您确实需要在线调试并通过 jquery 查看错误的创建位置:

就我而言,它存在于一个名为 ajaxConvert(s, response) 的 jquery 函数中

......一旦我在控制台中输入对象

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

TypeError: Object [object Object] has no method 'replace_html' on render rails 通过搜索我找到了stackoverflow问题的答案。再次被火洗礼!

于 2013-05-06T19:52:54.390 回答