kitz
kitz

Reputation: 889

Rails: Different modal for each link

I have this structure in my view:

<ul>
  <% @categories.each do |category| %>
    <li> <%= category.name %> <a href='#'>Edit</a> </li> 
  <% end %>
</ul>

Now, I want to popup a modal when I click "Edit" and in this modal to place a form for editing corresponding category. I want to avoid to generate a modal for each element from loop and I want to have a generic modal and call it each time, with specific parameters. Is this possible?

Upvotes: 3

Views: 819

Answers (2)

Sudarshan Dhokale
Sudarshan Dhokale

Reputation: 206

Use remote true for js response

<ul>
  <% @categories.each do |category| %>
     <li> <%= category.name %><%= link_to 'Edit', edit_category_path(category), remote: true %> </li> 
  <% end %>
</ul>
<div class='modal-fade' id="edit-modal"></div>

create one partial page _edit.html.erb write bootstarp modal structure and edit form and in form write remote: true

 eg. <%= form_for @category, remote: true do %>......<%end%>

create edit.js file and write as below

$('#edit-modal').html("<%= j render 'edit' %>");
$('#edit-modal').modal('show');

and create one more file create.js

$('#edit-modal').modal('hide');

Upvotes: 2

Pardeep Saini
Pardeep Saini

Reputation: 2102

just create a remote link:

link_to "edit", edit_category_path(category), class: "btn btn-mini", remote: true

Then in your views, add a edit.js.erb file:

$("#myModal").html("<%= j render "new"%>");
$('#myModal').modal('show');

and change your edit.html and new.html files to _new.html and edit.html

Upvotes: 0

Related Questions