2

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

8
  • 1
    DOM element IDs cannot be numbers and be valid HTML. Commented Jan 3, 2012 at 13:18
  • David, I changed it but I am still getting same issue Commented Jan 3, 2012 at 13:36
  • Does it render anything? Commented Jan 3, 2012 at 13:44
  • Yes, its rendering both the scripts each time, which generates a conflict ActionView::Template::Error (missing attribute: colName): Commented Jan 3, 2012 at 13:51
  • 1
    as per your ajaxview.js, there is a syntax 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")) %> Commented Jan 3, 2012 at 14:11

1 Answer 1

4

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 %>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mike, it was a nice explanation. And it did resolves my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.