DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Better Scaffolding with jQuery - Part I
  • Production-Grade React Project Structure: From Setup to Scale
  • Dynamic Web Forms In React For Enterprise Platforms

Trending

  • Ingesting Public Data That Fails Silently: Patterns From a TypeScript and Cloudflare Workers Pipeline
  • From Issue to Reviewable Patch: Build a Sandboxed Coding Agent With Deep Agents and Automated Tests
  • Vapi vs Retell AI vs LiveKit: Architecture, Latency, Pricing, and Developer Experience
  • Prompt Caching for LLMs, Part 3: Model-Side Caching and Production Rollout

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Creating Modals in Rails

Creating Modals in Rails

In this post, a developers gives a quick, step-by-step tutorial on how to create modals in your Rails application using JS and Ruby.

By 
Kristina Garcia Francisco
·
Feb. 14, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
50.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Instead of writing a JavaScript application for working with remote modals in your Rails application, a much simpler approach is to render our views on a server and display them as modals. In this tutorial, we will explain how to implement this by using Bootstrap modals.

Step 1: Initial Setup

Add gem 'bootstrap' and gem 'popperjs' to your gemfile. Also, in your application.js file, include this in the following order:

//= require jquery
//= require popper
//= require turbolinks
//= require bootstrap
//= require_tree.

Step 2: Modify Layouts in View

Make sure that this view is partial. Inside it, you will have the content you want to show in the modal.

_new.html.erb

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal">x</button>
  <h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
  *Modal content comes here*
</div>
<div class="modal-footer">
  <button class="btn btn-primary">Save</button>
</div>

We need to define a place where modals will be rendered:

index.html.erb

<%= link_to 'Add user', new_user_path,  {:remote => true, 'data-toggle' =>  "modal", 'data-target' => '#modal-window', class: 'btn btn-primary btn-lg'}  %>
<div id="modal-window" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content"></div>
  </div>
</div>

remote is used to tell jQuery to submit this form with AJAX, while 'data-toggle' => "modal"  is used to tell our script to handle this form as the modal form. <div> loads the partial as a modal window and with class="modal hide fade" you can add an extra fade effect to the modal, too!

To make this all work we need to add some JavaScript:

new.js.erb

$("#modal-window").find(".modal-content").html("<%= j (render 'new') %>");
$("#modal-window").modal();

Step 3: Modify Your Controller

In the controller add the respond_to block to use AJAX:

def new
  respond_to do |format|
    format.html
    format.js
  end
end

Step 4: Enjoy!

We have finally reached the end! Now you just need to click on the "Add user" button and a nice modal will show up with a fade effect! This is your end work result:

bootstrap-modal

Thank you for staying with us until the end!

application Form (document) workplace JavaScript AJAX GEM (desktop environment) Blocks Bootstrap (front-end framework) JQuery

Published at DZone with permission of Kristina Garcia Francisco. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Better Scaffolding with jQuery - Part I
  • Production-Grade React Project Structure: From Setup to Scale
  • Dynamic Web Forms In React For Enterprise Platforms

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook