Inertia Rails Contrib

repository·main·Indexed 41 days ago

https://github.com/skryukov/inertia_rails-contrib

A playground for new features and ideas for Inertia Rails. It provides integration for InertiaUI Modal, allowing developers to render components in modals or slideovers via the `inertia_modal` render method and custom HTTP headers.

Tokens
1.1K
Snippets
8
Records
8
Agent score
51%

What's inside Inertia Rails Contrib

  1. Enable InertiaUI Modal support

    main

    The inertia_rails-contrib gem provides integration for InertiaUI Modal, allowing you to open routes in Modals or Slideovers.

    To use this feature, you must first follow the NPM installation instructions from the InertiaUI Modal documentation. Then, enable the feature in your Rails application via the inertia_rails_contrib.rb initializer.

    InertiaRailsContrib.configure do |config|
      config.enable_inertia_ui_modal = true
    end
  2. Use the `inertia_modal` render method

    main

    Once enable_inertia_ui_modal is set to true, you can use the inertia_modal render method in your controller actions. This allows you to render a component inside a modal without changing your existing routes or controllers.

    You can provide a base_url to support updating the URL when the modal is opened.

    class PostsController < ApplicationController
      def new
        render inertia_modal: 'Post/New', props: { post: Post.new }, base_url: posts_path
      end
    end
  3. Handle redirects within Inertia UI Modals using redirect_back_or_to

    main

    When working with Inertia UI Modals, standard Rails redirects might not behave as expected because the request originates from within a modal context. The InertiaRailsContrib::InertiaUIModal::Redirect module provides an override for redirect_back_or_to.

    This method checks for the presence of the HEADER_BASE_URL in the request headers. If this header is present and the host is allowed, the redirect is sent back to the URL specified in the modal's base URL header, ensuring the user is returned to the correct underlying page rather than just the previous entry in the browser history. If the header is missing or the host is not allowed, it falls back to the standard Rails redirect_back_or_to behavior.

    # This method is intended to be included in a controller context
    # to enable modal-aware redirects.
    
    redirect_back_or_to(fallback_location, allow_other_host: false, **options)