onrails.org home

.rjs and link_to_function

A neat little trick is that .rjs templates can be used to generate local javascript functions and be invoked without doing a server roundtrip.

playground_controller.rb

class PlaygroundController < ApplicationController
def rjs
response.headers[‘content-type’] = ‘text/html’
end
end

rjs.rhtml



<%= javascript_include_tag :defaults %>

Javascript function test

<%= link_to_function(‘Add’, ‘add_item()’ ) -%> |
<%= link_to_function(‘Clear’, ‘clear_list()’) >

Fork me on GitHub