The js part should be located in an other file and then included in the webpage with javascript_include_tag.