iCyborg
iCyborg

Reputation: 4728

How to use bootstrap Modal with link_to in rails?

I had asked this earlier but unfortunately I am still stuck. I have a link_to like this

<%= link_to "Click Here", page_path,, :id => 'login', "data-toggle" => "modal" %>

in page.html.erb (the page which I want to load in modal when the link gets clicked),I have

<div class="modal" id="loginModal">
    Test Content
</div>

in assets/page.js.coffee

$('#loginModal').modal(options)

but still, the link is not opening in a modal Any help ?

Upvotes: 16

Views: 25937

Answers (3)

Jhonnatas Alencar
Jhonnatas Alencar

Reputation: 113

Using Rails 7 with Bootstrap 5

 <%= link_to 'Add user', '#',  {:remote => true, 'data-bs-toggle' =>  "modal", 'data-bs-target' => '#exampleModal', class: 'nav-link'}  %>

Upvotes: 1

kobaltz
kobaltz

Reputation: 7070

There is a prettier way to add data attributes in Rails. You can do something like this to get the same results.

<%= link_to 'Click Here', "#", data: {toggle: "modal", target: "#modal"} %>

Upvotes: 11

Intrepidd
Intrepidd

Reputation: 20878

Add data-target

<%= link_to "Click Here", page_path, :id => 'login', "data-toggle" => "modal", 'data-target' => '#loginModal' %>

Upvotes: 18

Related Questions