本文介绍了Ruby如果条件在JavaScript文件中不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Controller: def AjaxView @ vars = Var.find(:all,:conditions => {:varName =>one},:select =>(params [:col])) @col = params [:col] respond_to do | format | format.js {render:layout => false} 结束结束 AjaxView.js.erb if('<%= @col%>'==' (<%= escape_javascript(render(:partial =>var))%>); } else if('<%= @col%>'=='colName2'){ $(#2)。text(<%= escape_javascript(render partial =>var1))%>); $ / code> 查看部分: _var.html.erb <%= @vars [0] .colName%> _var1.html.erb <%= @vars [0] .colName2%> 如果我更改此代码 $(#3)。text(<%= escape_javascript(render(:partial =>var)%>); $ b 到 alert(hi_one); 和 $(#2)。text(<%= escape_javascript(render(:partial =>var1)%>); 改为 alert(hi_two); 它工作的很好。 但是当我把上面的代码,它每次都运行代码,不知道为什么,它编译它还是什么?和什么出路? 渲染输出: 呈现的测试/ _var.html.erb(16.0ms )渲染测试/ AjaxView.js.erb(19.0ms)在38ms内完成500个内部服务器错误 ActionView :: Template :: Error (缺少属性:colName): 1:<%= @ vars [0] .col%> $ b Firebug中的Javascript输出为500错误 p> 解决方案目前您的条件 if('<%= @col%>'=='colName')是客户端 JavaScript。 这意味着在 if('<% = @col%>'=='colName'){ $(#3)。text(<%= escape_javascript(render(:partial => var))%>); } else if('<%= @col%>'=='colName2'){ $(#2).text (<%= escape_javascript(render(:partial =>var1))%>); } 都是渲染器:部分调用被执行服务器端,以便Rails呈现模板,但随后当生成的JavaScript执行客户端时,逻辑决定执行哪个 .text 调用,这意味着两个部分的内容在两种情况下都必须是有效的(即使是最终) 要解决它,要么包含 colName 和:select 中选择c> colName2 ,或者如果改为为服务器端然后你只用 .text 行中的1来回应。例如 $(#3 >); <%els如果@col =='colName2'%> $(#2)。text(<%= escape_javascript(render(:partial =>var1))%>); <%end%> Controller:def AjaxView @vars= Var.find(:all,:conditions => { :varName=> "one" },:select=>(params[:col])) @col = params[:col] respond_to do |format| format.js { render :layout=>false } endendAjaxView.js.erb if('<%= @col %>' == 'colName'){ $("#3").text("<%= escape_javascript(render(:partial => "var")) %>"); } else if('<%= @col %>' == 'colName2'){ $("#2").text("<%= escape_javascript(render(:partial => "var1")) %>"); }View Partial:_var.html.erb <%= @vars[0].colName %>_var1.html.erb <%= @vars[0].colName2 %>If I change this code$("#3").text("<%= escape_javascript(render(:partial => "var") %>"); to alert("hi_one"); and$("#2").text("<%= escape_javascript(render(:partial => "var1") %>");to alert("hi_two"); it works fine.but when I put the above code, it runs both the code each time, not sure why, is it compiling it or what ? and whats the way out?Render Output:Rendered test/_var.html.erb (16.0ms)Rendered test/AjaxView.js.erb (19.0ms)Completed 500 Internal Server Error in 38msActionView::Template::Error (missing attribute: colName): 1: <%= @ vars[0].col %>Javascripty output in Firebug is 500 error 解决方案 Currently your condition if('<%= @col %>' == 'colName') is client-side JavaScript.What this means is that in if('<%= @col %>' == 'colName'){ $("#3").text("<%= escape_javascript(render(:partial => "var")) %>");} else if('<%= @col %>' == 'colName2'){ $("#2").text("<%= escape_javascript(render(:partial => "var1")) %>");}both render :partial calls are executed server-side in order for Rails to render the template but then when the generated JavaScript executes client-side the logic determines which .text call executes. This means that the contents of both partials must be valid in both scenarios (even the one that eventually isn't going to be used.)To fix it, either include both colName and colName2 in your :select or change the if to be server-side so that you then respond with just one 1 of the .text lines. e.g.<% if @col == 'colName' %> $("#3").text("<%= escape_javascript(render(:partial => "var")) %>");<% elsif @col == 'colName2' %> $("#2").text("<%= escape_javascript(render(:partial => "var1")) %>");<% end %> 这篇关于Ruby如果条件在JavaScript文件中不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 13:11