Answer the question
In order to leave comments, you need to log in
How to make a reusable modal window in Rails?
I'm new to Rails, trying to make a reusable modal window. The bottom line is to make a container and import the content I need through yield
.
PS: HTML code will be written using HAML. I also use the bootstrap-modal-rails gem to render windows.
This is what my window container looks like:
%div(class="modal" id="mainModal ajax-modal" tabindex="-1" role="dialog"){:'aria-labelledby'=>"mainModalLabel", :'aria-hidden'=>"true"}
%div(class="c-modal__container")
%div(class="c-modal__layout modal__content")
= yield
application.html.haml
I have div
an element that further displays my dialog box:-# ...code...
%div(id="modal-holder")
- unless params[:nojs]
= javascript_include_tag 'desktop'
-# ...code...
book controller
. Each user can add a book, edit its information, and so on. I'm using multi-step registration, in other words, when the user clicks add a book , I should have a dialog box with a registration form, which (if the form is completed successfully) takes me to the additional information page . = link_to "New book", new_book_path, class: "c-link--navbar", data: { modal: true }
$(function() {
var modal_holder_selector, modal_selector;
modal_holder_selector = '#modal-holder';
modal_selector = '.modal';
$(document).on('click', 'a[data-modal]', function() {
var location;
location = $(this).attr('href');
$.get(location, function(data) {
return $(modal_holder_selector).html(data).find(modal_selector).modal();
});
return false;
});
return $(document).on('ajax:success', 'form[data-modal]',
function(event, data, status, xhr) {
var url;
url = xhr.getResponseHeader('Location');
if (url) {
window.location = url;
} else {
$('.modal-backdrop').remove();
$(modal_holder_selector).html(data).find(modal_selector).modal();
}
return false;
});
});
-# ...code...
= form_for(@book, remote: remote, html: {role: :form}, class: "c-form") do |f|
-# Forms here...
= f.submit "Add a new book", class: "c-btn"
-# ...code...
book controller
:class BooksController < ApplicationController
respond_to :html, :json, :js
...
def index
@books = current_user.books
end
def new
@book = current_user.books.build
respond_modal_with @book
end
def create
@book = current_user.books.build(book_params)
if @book.save
redirect_to general_book_path(@book), notice: "Saved."
else
flash.now[:notice] = "Something went wrong."
render :new
end
end
...
end
respond_modal_width
method:class ModalResponder < ActionController::Responder
cattr_accessor :modal_layout
self.modal_layout = 'modal'
def render(*args)
options = args.extract_options!
if request.xhr?
options.merge! layout: modal_layout
end
controller.render *args, options
end
def default_render(*args)
render(*args)
end
def redirect_to(options)
if request.xhr?
head :ok, location: controller.url_for(options)
else
controller.redirect_to(options)
end
end
end
jQuery
, but I need to understand how to achieve a similar result with Ruby on Rails
. I want to note that in yeild
is loaded directly by itself action
, and not by partial
. On this occasion, I have a second problem related to the fact that it jQuery code
does not work inside my window. Answer the question
In order to leave comments, you need to log in
This is very easy to do on the rail, if you know how. It’s a long time to paint, but I’ll tell you what to look for
remote forms and links
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question