Topic: New to Rails and stuck on check_box

Greetings fellow Rails programmers!

I'm creating this thread hoping that I could get some answers to the problem that I'm currently facing. I've written a tiny application(learning purposes) that keeps track of generic id numbers.

The name of the model is Check and the corresponding controller is called CheckController. The view that pulls out a list of all items is the Index.rtml file. It has your everyday 'show', 'edit', and 'destroy' actions associated with it. The delete action is carried out by a button that is present on every row.

My problem is when the list gets too big, it would be tedious for someone to go in and delete every row individually. I was wondering if there was a way to implement check_box method to let the user select multiple items and delete them using just one button. Below is some of the code that I have come up with. If anyone can give me advice on how to proceed or point me to some resources that would let me accomplish this goal, I would greatly appreciate it! smile

The index.rhtml file:

<table>
  <tr>
    <td><%= button_to 'Select all' %></td>
   
    <td><%= button_to 'Delete' %></td>
  </tr>
  <tr>
    <th>Name</th>
    <th>Edit</th>
    <th>Delete</th>
  </tr>
 
  <%= render :partial => 'check', :collection => @checks %>
</table>

<p><%= link_to 'Add a new Check', :action => 'new' %></p>


The _check.rhtml partial:

<tr>
  <td><%= link_to check.check_name, :action => 'show', :id => check %></td>
  <td><%= link_to 'Edit', :action => 'edit', :id => check %></td>
  <td>
    <%= check_box('check', 'destroy', {}) %>
  </td>
</tr>

I have the regular "destroy" method in the CheckController:

def destroy
    @check = Check.find(params[:id])
    flash[:notice] = "Successfully deleted Check"
    @check.destroy
    redirect_to :action => 'index'
end

Last edited by RailsRocks (2006-12-27 01:00:10)

http://www.webyfi.com
It's not the size of the app that matters, it's how you code it. ~ railsenvy

Re: New to Rails and stuck on check_box

ok, you are going to need to do some recoding to get it to work.

try this:

index.rhtml:

<%= form_tag :action => 'destroy' %>

<table>
  <tr>
   <td><%= button_to 'Select all' %></td>
   
    <td><%= button_to 'Delete' %></td>
   </tr>
  <tr>
     <th>Name</th>
     <th>Edit</th>
     <th>Delete</th>
  </tr>
   
  <%= render :partial => 'check', :collection => @checks %>
</table>
   <%= submit_tag "Update" %>
<%= end_form_tag %>
<p><%= link_to 'Add a new Check', :action => 'new' %></p>


_check.rhtml partial:
 <tr>
   <td><%= link_to check.check_name, :action => 'show', :id => check %></td>
   <td><%= link_to 'Edit', :action => 'edit', :id => check %></td>
   <td>
     <%= check_box_tag("check_destroy[check_#{check.id}]") %>
   </td>
</tr>

CheckController:
def destroy
  params[:check_destroy].each do |k,v|
    Check.find(k.split("_").last).destroy
  end

end


hope that helps

--jake

Last edited by jake3030 (2006-12-27 20:58:23)

---------------------------------------------------------------------
Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.

Re: New to Rails and stuck on check_box

Wow, thanks for the help jake3030! This was just what I was looking for! smile

http://www.webyfi.com
It's not the size of the app that matters, it's how you code it. ~ railsenvy