Reputation: 337
I am working on a ruby sinatra app and wanted to know if it was possible to inject some javascript at the end of one my erb files from my routes.rb file. I have seen some ruby gems do this before but haven't been able to find a way to do this.
Any help would be appreciated.
Alex
Upvotes: 2
Views: 924
Reputation: 61
You can take a look at how you can use content_for
from rails and use it in your project: http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
I think you can grab this source of the method:
def content_for(name, content = nil, options = {}, &block)
if content || block_given?
if block_given?
options = content if content
content = capture(&block)
end
if content
options[:flush] ? @view_flow.set(name, content) : @view_flow.append(name, content)
end
nil
else
@view_flow.get(name).presence
end
end
Then use it like this:
in your view:
<%= content_for :my_awesome_js %>
in .rb file:
content_for :my_awesome_js do
"<script>
$(document).ready(function() {
alert('Hello world!');
});
</script>"
end
Upvotes: 0
Reputation: 61
You can inject javascript in your .erb files like this:
<script>
$(document).ready(function() {
alert('Hello world!');
});
</script>
Upvotes: 0