我试图在我的页面上呈现部分在控制台或终端日志中没有错误的情况是......用户选择页面上的链接并且中间区域应该呈现。我正在运行这个通过jquery并让提交进入我的控制器'session'并且方法'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)
感谢您的帮助
答案 0 :(得分:0)
$(#middle_area').replace_html( "<%= escape_javascript render :partial => "middle_survey" %> ");
看起来像是一个错字,你忘了转发引号。
$(的 强> #middle_area')
另外,我不是很擅长CSS,但我看不到div #middle_area
本身,你确定jquery可以解析#middle_area-right
等等吗?
答案 1 :(得分:0)
发现问题所有精彩的jquery返回像“replace_html”等不起作用,因为当你从控制器渲染js时,它认为它是普通的旧js,因此它返回到原型对象(深埋在jquery中)我实际上通过阅读另一个stackoverflow问题回答“用jquery-rails替换Prototype - replace_html导致错误”来找到这个“
底线是你真的需要在线调试并通过jquery来查看错误的创建位置:
就我而言,它存在于...的功能中 jquery调用ajaxConvert(s,response)
......一旦我在控制台中输入了对象
conv( response ) it displayed the following .....
TypeError:Object [object Object]在render rails上没有方法'replace_html' 通过搜索,我找到了stackoverflow问题的答案。 再次受到火灾的洗礼!