3

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?

Flip
  • 6,233
  • 7
  • 46
  • 75
kitz
  • 879
  • 2
  • 9
  • 24

2 Answers2

2

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');
0

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

Pardeep Saini
  • 2,084
  • 1
  • 18
  • 29