Creating Rails Modals Using Bootstrap (2024)

Instead of writing your own JavaScript code for handling remote modals in your Rails application, it is a much simpler approach to implement it by using Bootstrap. In this tutorial, we will explain how to do it efficiently.

Kristina P.

2 min read

Creating Rails Modals Using Bootstrap (2)

Instead of writing your own JavaScript code for handling remote modals in your Rails application, it is a much simpler approach to implement it by using Bootstrap. In this tutorial, we will explain how to do it efficiently.

Step 1: Initial Setup

Add gem 'bootstrap' and gem 'popperjs' to your Gemfile and run bundle install.
After installing the gems, update your application.js 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! All that is left is to position the modal the way you want.

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 be able to use Ajax:

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

Step 4: Enjoy!

We have finally reached the end! Now you just need to click on "Add user" button and a nice modal will show up with a fade effect! The only thing that might be left is to add some Model and Controller tests, be sure to check how to get started with RSpec and FactoryGirl!

This is our final result:

Creating Rails Modals Using Bootstrap (3)

Thank you for staying with us until the end.

If you want to read more about Rails, be sure to subscribe to our newsletter!

Creating
software solutions
that work

Creating Rails Modals Using Bootstrap (4) Creating Rails Modals Using Bootstrap (5) Creating Rails Modals Using Bootstrap (6) Creating Rails Modals Using Bootstrap (7) Creating Rails Modals Using Bootstrap (8)

Explore topics

css

32

ruby on rails

21

web design

17

examples

12

Creating Rails Modals Using Bootstrap (2024)
Top Articles
Service account credentials  |  IAM Documentation  |  Google Cloud
Hide and unhide music, movies, TV shows, audiobooks, and books - Apple Support (CA)
11 beste sites voor Word-labelsjablonen (2024) [GRATIS]
7 Verification of Employment Letter Templates - HR University
Summit County Juvenile Court
Santa Clara College Confidential
Tyrunt
What happens if I deposit a bounced check?
Khatrimaza Movies
Hardly Antonyms
Full Range 10 Bar Selection Box
Olivia Ponton On Pride, Her Collection With AE & Accidentally Coming Out On TikTok
Craigslist Heavy Equipment Knoxville Tennessee
Thotsbook Com
How Many Cc's Is A 96 Cubic Inch Engine
Oro probablemente a duna Playa e nomber Oranjestad un 200 aña pasa, pero Playa su historia ta bay hopi mas aña atras
Apne Tv Co Com
Telegram Scat
Dignity Nfuse
Hollywood Bowl Section H
Weepinbell Gen 3 Learnset
Metro Pcs.near Me
Pjs Obits
Between Friends Comic Strip Today
Masterkyngmash
Holiday Gift Bearer In Egypt
THE FINALS Best Settings and Options Guide
Sadie Sink Reveals She Struggles With Imposter Syndrome
kvoa.com | News 4 Tucson
Sound Of Freedom Showtimes Near Movie Tavern Brookfield Square
Craigslist Rome Ny
Unreasonable Zen Riddle Crossword
Xxn Abbreviation List 2023
Gesichtspflege & Gesichtscreme
Eaccess Kankakee
Tributes flow for Soundgarden singer Chris Cornell as cause of death revealed
Lucky Larry's Latina's
2016 Honda Accord Belt Diagram
The Land Book 9 Release Date 2023
Die Filmstarts-Kritik zu The Boogeyman
Wisconsin Women's Volleyball Team Leaked Pictures
Oxford House Peoria Il
Craigslist Putnam Valley Ny
301 Priest Dr, KILLEEN, TX 76541 - HAR.com
Lonely Wife Dating Club בקורות וחוות דעת משתמשים 2021
How Much Is 10000 Nickels
Here's Everything You Need to Know About Baby Ariel
Frequently Asked Questions
Abigail Cordova Murder
Doelpuntenteller Robert Mühren eindigt op 38: "Afsluiten in stijl toch?"
Aspen.sprout Forum
Duffield Regional Jail Mugshots 2023
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5667

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.