Why isn't this passing text to javascript/jquery.? @i[:error]
definitely has a string in it, I can print that on console.
js.erb file -
<% if @i[:error] != "" %>
<% puts "--> " + @i[:error] %>
#--> error
#Validation error(s):
#- Item Is not defined or blank. # that's the error string in @i[:error]
$(function() {
$("#error_message").attr("class", "message_error");
$('#error').text("<%= @i[:error]%>"); #Not working for @i[:error]
#$('#error').text("<%= "#{@i[:error]}"%>");#Not working for @i[:error]
#$('#error').text("Test"); #This is working
#$('#error').text("<%= "?????"%>"); #This is working
});
<% else %>
........#fine here
<% end %>
Yes, it's probably because of the line break. You can fix it by using
escape_javascript
.So:
If you specify what debugger says it would be easier to understand the problem. This code for erb views works fine:
Pay attention that emptiness of string is checked by blank? method, I think it look much nicer than using != operator.