S
S
sergeyevanton2017-12-29 00:39:03
JavaScript
sergeyevanton, 2017-12-29 00:39:03

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


In mine application.html.hamlI have divan element that further displays my dialog box:

-# ...code...
%div(id="modal-holder")

- unless params[:nojs]
  = javascript_include_tag 'desktop'

-# ...code...


This is where my problem comes from. I have 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 in my menu:

= link_to "New book", new_book_path, class: "c-link--navbar", data: { modal: true }


Modal window code:

$(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;
  });
});


When you click on a link or button, a window pops up with a form:

-# ...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...


In general, everything works correctly, however, if I make an error when entering, it throws me to a separate page , where a similar form is located. In other words, my window becomes a separate page.

My 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_widthmethod:

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


Actually, my task is that in case of an error, the user is not thrown to a new page, but left on the same one, with an open dialog box. I am well aware that a similar check can be done with a regular jQuery, but I need to understand how to achieve a similar result with Ruby on Rails. I want to note that in yeildis loaded directly by itself action, and not by partial. On this occasion, I have a second problem related to the fact that it jQuery codedoes not work inside my window.

I asked a similar question on StackOverflow: Reusable remote modal Rails

AND: jQuery UI autocomplete in remote modal RoR


Thank you so much for your time and help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zaporozhchenko Oleg, 2017-12-29
@c3gdlk

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 question

Ask a Question

731 491 924 answers to any question