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 }
end
end
AjaxView.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 38ms
ActionView::Template::Error (missing attribute: colName): 1: <%= @ vars[0].col %>
Javascripty output in Firebug is 500 error
<%= escape_javascript(render(:partial => "var") %>should be <%=escape_javascript(render(:partial => "var")) %>and<%= escape_javascript(render(:partial => "var1") %>should be<%= escape_javascript(render(:partial => "var1")) %>