The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Thursday, October 18, 2007

Rails RJS and newline characters

ok this one really got me, I was generating a javascript alert from the server via an rjs file, i.e.

# rjs render a javascript alert on the client
page.alert 'Errors\nError 1\nError 2'

and it kept on printing my error message as Errors\nError message 1\nError message2 - note that the newline characters were being escaped and rendered literally

I found this informative post which lead me to change my code from using single quotes to using double quotes - nasty.

# this one will output the newlines in the javascript alert message
page.alert "Errors\nError 1\nError 2"



here is the final code, looping through each error on the model object (product in this case) and outputing a newline on the javascript alert for each error


# rjs file
page.alert "#{get_message_for_show_server_error}"

# helper file
def get_message_for_show_server_error
__s = "Errors "
__@product.errors.full_messages.each do error
____s = s + "\n" + error.to_s
__end

__return s
end


No comments: