4

I am attempting to clean up my code a bit by placing my modal code inside of a partial, however I am encountering problems when trying to call the modal.

I tried the solution provided by How to show twitter bootstrap modal via JS request in rails?, but if I understand correctly I have to add code to a particular controller to display the modal. That would be fine however I would like to display the modal regardless of the controller/view it is in. Is this possible?

What I have (completely from the above question):

current link (views/static_pages)

<%= link_to "ModalTest", 'shared/testmodal', {:remote => true, 'data-toggle' => 'modal', 'data-target' => "testmodal" }%>

show.js.erb

$('.modal-body').html('<%= escape_javascript(render :partial => 'shared/testmodal'%>');
$('.modal-header').remove(); 

previous attempt:

<%= link_to "ModalTest", render(:partial => 'shared/testmodal') %>
Community
  • 1
  • 1
SomberClock
  • 151
  • 1
  • 1
  • 13

1 Answers1

11

You can include the modal contents you put in the partial, in any view you need or in your application.html, with:

<%= render "shared/testmodal" %>

And toggle it with:

<%= link_to "ModalTest", "#testModal", "data-toggle" => "modal" %>

Where #testModal is your modal id.

albertedevigo
  • 18,262
  • 6
  • 52
  • 58
  • It worked for the given example, but I've got one question. As you said you wanted to "display the modal regardless of the controller", how did you do that part to load the instance variables from the right controller asynchronously? – Nuno Costa Jul 15 '16 at 12:23
  • I created the variable inline in the form itself – Nuno Costa Jul 15 '16 at 13:11