Important: This version was made for use with Rails >= 3.1. If you are using Rails 2.3, please take a look
As a gem
rjs_helpers as a gem, run
gem install rjs_helpers
or put this into your
In this version, RJS helpers is built as a Rails engine, so the necessary helper methods and assets are automatically loaded into your application.
You only have to extend your
//= require rjs_helpers
The plugin consists of two parts:
- A Rails helper which is available in application views
- A JS helper library which has to be included into your application scripts / layout
The JS file which is part of this plugin uses jQuery to perform its tasks. If your application is using a different framework, feel free to write an own adapter for it.
Update, Replace, Append, Prepend
These content update functions always take a selector element and the new content.
The selector element may either be a string/symbol which is passed directly to the JS function
or something the
dom_id() function would take as well (an instance of ActiveRecord or an Array).
The content may either be a string containing the new content or options for the
Updates the given element with the given content
Replaces the given element with the new content
Appends the given content to the given container element
append, but at the top of the container element
Removing DOM elements
Removes the given element from the DOM.
Show, Hide and Toggle
Shows the given element, by default with
Hides the given element, by default with
Toggles the given element, by default with
The argument may be everything that the
url_for() function would accept, e.g.
[:admin, :users] or
js_scroll_to(element, offset_top = 0)
Scrolls to the given element on the page. The optional second parameter can be used to set a top offset. This is e.g. useful if you have a fixed top navbar (greetings to you, bootstrap).
:top is given as the first argument, the page will simply scroll to the very top (+ offset).
= js_update_element @user, :partial => 'user', :object => @user #=> rjsHelpers.updateElementByDomId('user_123', ...) #=> jQuery('#user_123').html(...) = js_update_element [@user, :emails], :partial => 'emails' #=> rjsHelpers.updateElementByDomId('emails_user_123', ...) #=> jQuery('#emails_user_123').html(...) = js_hide_element '#myContainer' #=> rjsHelpers.hideElement('#myContainer') #=> jQuery('#myContainer').hide() = js_replace_element '#willBeOverridden', 'And now for something completely different' #=> rjsHelpers.replaceElement('#willBeOverridden', 'And now for something completely different') #=> jQuery('#willBeOverridden').replaceWith('And now for something completely different')
Copyright (c) 2013 Stex, released under the MIT license