Rails loading modals

32 views Asked by At

I have some views which require a lot of partials, which in turn require several bootstrap modals to work correctly.

However, loading modals is a bit painful, because if I load them in the wrong tag, it can completely screw them up, and duplicate them.

I'd like to have a nice helper load_modal, which would actually save a list of modals which need to be loaded. Then, in a part of my HTML of my choosing, I can choose to iterate over this list to load the partials

MY PROBLEM

How can I store the list of partials to be called AS WELL AS THEIR ARGUMENTS ?

I have thought of the following, (deliberately using non-working syntax as I didn't know how to do otherwise)

application_helper.rb

# Input should look like 
# => 'path_to_partial', {locals}
def load_modal(*args)
  unless (@modals ||= []).include?(args.first)
    @modals << args
  end
end

some_partial.html.erb

<% load_modals('project/deadline', project: @project) %>

application.html

<% if @modals
    @modals.each do |modal| %>
        <%= render(modal) %>
    <% end %>
<% end %>

Note : the current error with this code is

'"project/deadline"' is not an ActiveModel-compatible object. It must implement :to_partial_path.
1

There are 1 answers

0
Cyril Duchon-Doris On BEST ANSWER

This seems to work

application_helper.rb

  def load_modal(*args)
    unless (@modals ||= []).include?(args.first)
      @modals.push(*args)
    end
  end

application.html.erb

<% if @modals
    @modals.each do |modal_with_args| %>
      <%= render(modal_with_args) %>
    <% end %>
<% end %>