Tuesday, December 14, 2010

Returning Javascript along with HTML from Rails Views

One way is as follows -

< %= javascript_tag :defer => 'defer' do -%>
alert('this is a view.html.erb')
< % end -%>



Another way is as follows
This goes in the application.html.erb

< script type="text/javascript">
$(document).ready(function() {
<%= yield :javascript %>
});

< /script>


And in the view we use content_for

< %= content_for :javascript do -%>
alert($)
< % end -%>

No comments:

Post a Comment