jquery-rails Documentation

repository·master·Indexed 21 days ago

https://github.com/rails/jquery-rails

A Ruby gem that integrates jQuery into Rails applications. It provides jQuery versions 1, 2, and 3, the jQuery UJS adapter, and the `assert_select_jquery` testing helper for verifying jQuery method calls in Ruby tests.

Tokens
952
Snippets
3
Records
5
Agent score
26%

What's inside jquery-rails

  1. Understand jquery-rails versioning behavior

    master

    Since v2.1, jquery-rails follows specific versioning guidelines to give you control over the bundled jQuery version via your Gemfile:

    • Patch version bump: Updates to jquery-ujs, jquery-rails, and patch-level updates to jQuery.
    • Minor version bump: Minor-level updates to jQuery.
    • Major version bump: Major-level updates to jQuery and updates to Rails which may be backwards-incompatible.
  2. Configure jQuery assets in the Rails asset pipeline

    master

    After installing the gem, you must require the jQuery files in your JavaScript asset manifest (e.g., app/assets/javascripts/application.js).

    Standard Setup (jQuery 1.x)

    To use the default jQuery version and the UJS adapter:

    //= require jquery
    //= require jquery_ujs

    Rails 5.1+ with rails-ujs

    If you are using Rails 5.1 or higher and have already included rails-ujs, you do not need jquery_ujs. Simply require jQuery:

    //= require jquery

    Using jQuery 2

    To specifically use jQuery 2:

    //= require jquery2
    //= require jquery_ujs

    Using jQuery 3

    To specifically use jQuery 3:

    //= require jquery3
    //= require jquery_ujs
  3. Install jquery-rails

    master

    To use jQuery within a Rails application, add the jquery-rails gem to your Gemfile.

    This gem provides jQuery (versions 1, 2, or 3), the jQuery UJS adapter, and assert_select_jquery for testing jQuery responses in Ruby tests.

    gem 'jquery-rails'
  4. Use jQuery UI with Rails

    master

    As of version 3.0, jquery-rails no longer includes jQuery UI.

    To use jQuery UI, it is recommended to use the jquery-ui-rails gem, which includes the necessary jQuery UI CSS and allows for easier customization.

  5. Use `assert_select_jquery` to test jQuery responses

    master

    The assert_select_jquery method is a testing helper used to assert that a response contains specific jQuery method calls. It can be used to verify that elements are being manipulated (e.g., shown, hidden, or appended to) via JavaScript in your Rails tests.

    Narrowing Assertions

    You can refine your assertion by providing specific arguments:

    • No arguments: Asserts that one or more jQuery method calls are made.
    • method (Symbol): Narrows the assertion to only statements calling that specific method (e.g., :hide).
    • opt (Symbol): Narrows the assertion to only statements that pass this option as the first argument.
    • id (String): Narrows the assertion to only statements that invoke methods on the result of using this identifier as a selector.

    Using Blocks for HTML Content

    • Without a block: Simply asserts that the response contains a matching jQuery statement.
    • With a block: Asserts that the method call passes a JavaScript-escaped string containing HTML. The method extracts these HTML fragments and passes them into the block, allowing you to perform nested assertions (like assert_select) on the content itself.
    # Asserts that the #notice element is hidden
    assert_select :hide, '#notice'
    
    # Asserts that the #cart element is shown with a :blind parameter
    assert_select :show, :blind, '#cart'
    
    # Asserts that #cart content contains a #current_item (using a block to inspect HTML)
    assert_select :html, '#cart' do
      assert_select '#current_item'
    end
    
    # Asserts that a .product is appended to a #product_list
    assert_select_jquery :appendTo, '#product_list' do
      assert_select '.product'
    end