Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render Bootstrap modal from partial in rails

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') %>
like image 677
SomberClock Avatar asked Dec 21 '22 16:12

SomberClock


1 Answers

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.

like image 189
albertedevigo Avatar answered Jan 04 '23 08:01

albertedevigo