Topic: Trying to Edit or Delete with a Drop Down

My list_category.rhtml doesn't work.  Trying to select a category/subcategory from a drop down, and then hit edit or delete for that category.  When I hit edit or delete, it edits or deletes the wrong category.  In addition, I need some error checking.  If I try to delete a category that has children, then I need a message:"Can't delete category, it has children".  So the only time I would be able to delete a category is if it has no children or products under it.  Any help much appreciated.  I can provide more code if you need it, but here is the list_category.rhtml

list_category.rhtml

<div align="left">
  <table width="550" border="0" cellspacing="0" cellpadding="0" align="center"><tr><td colspan="3"><h2 align="left">Category Manager</h2>
        <p align="left"><strong>Category</strong></p></td></tr>
   
    <tr>
      <td align="left" valign="middle" colspan="3" ><select name="categories[id]">
        <option value=" " >Top Level </option>
        <% for category in @categories %>
        <option value="<%= category.id %>" <%= ' selected' if category.id == @categories_id %>>
        <% for ancestor in category.ancestors.reverse %>
        <%=h ancestor.name %> >
          <% end %>
        <%=h category.name %> </option>
        <% end %>
      </select></td>
    </tr>
    <tr>
      <td align="left" valign="middle" width="34" ><%= button_to 'Edit', :action => 'edit_category', :id => category %></td>
      <td align="left" valign="middle" width="32" ><%= button_to 'Delete', { :action  => 'destroy_category', :id => category },                         :confirm => "Are you sure?",                         :post    => true %></td>
      <td align="left" valign="middle" width="484" ><%= button_to 'Add', :action => 'new_category' %> </td>
    </tr>
   
   
  </table>
</div>

Re: Trying to Edit or Delete with a Drop Down

All Ruby code is evaluated before the page is even sent to the user, so there's no way the "category" variable which is set as the :id could be the value that the user selected.

One way is to do this through JavaScript. But, since I don't like javascript, I won't be providing any code for that. wink

Another way is to put it all in one form and give each button a unique name. The button that is clicked will be sent in with the form so you can determine what action to take. For example:

<%= start_form_tag :action => 'act_on_category' %>
  <%= select_tag :id, options_from_collection_for_select(@categories, :id, :name) %>
  <%= submit_tag 'Edit', :name => 'edit_button' %>
  <%= submit_tag 'Destroy', :name => 'destroy_button' %>
<%= end_form_tag %>

def act_on_category
  @category = Category.find(params[:id])
  if params[:edit_button]
    #...
  else
    #...
  end
end

The solution isn't as clean as I would like, but it should work at least.

Railscasts - Free Ruby on Rails Screencasts

Re: Trying to Edit or Delete with a Drop Down

Thanks again Ryan.  Can you recommend a good Ruby on Rails book that covers ecommerce in detail?  I've been getting a lot of good help from this user group, but I'm still in the dark on how some of Ruby syntax works.  As you could probably tell from the questions I've asked you, I'm trying to develop my own cms/cart.

Re: Trying to Edit or Delete with a Drop Down

How would I go about that error checking?

Re: Trying to Edit or Delete with a Drop Down

Not sure how that works with the unlimited categories acts as tree.

Re: Trying to Edit or Delete with a Drop Down

I haven't read any ecommerce specific books about rails so I don't know. There's this book, but I don't know how good it is. I think there's a couple other e-commerce books, but I'm not sure if they are in print yet.

Railscasts - Free Ruby on Rails Screencasts

Re: Trying to Edit or Delete with a Drop Down

rdx73 wrote:

Not sure how that works with the unlimited categories acts as tree.

It should just work with acts_as_tree, it is just that all categories will appear in one flat list (you can order it any way you want to).

Railscasts - Free Ruby on Rails Screencasts

Re: Trying to Edit or Delete with a Drop Down

rdx73 wrote:

How would I go about that error checking?

Not sure what you mean by this. If the edit button is clicked, it should probably redirect to the edit page and you can then handle error checking as normal. I don't think you will need error handling when destroying.

Railscasts - Free Ruby on Rails Screencasts

Re: Trying to Edit or Delete with a Drop Down

Well, actually I do need error checking on destroying a category.  What if that category, has sub categories or products attached to it?  Then I would have products that were miscategorized. 

Another question, how would I go about adding a product to more that one category or subcategory?

Re: Trying to Edit or Delete with a Drop Down

rdx73 wrote:

Well, actually I do need error checking on destroying a category.  What if that category, has sub categories or products attached to it?  Then I would have products that were miscategorized.

You may want to perform these checks in the controller, add a flash message if there's an error, and redirect back to the other page.

rdx73 wrote:

Another question, how would I go about adding a product to more that one category or subcategory?

You need to create a many-to-many association between categories and products. This can be done by creating a categories_products table and using has_and_belongs_to_many in the models. Or you can create a full-blown join model and use has_many :through. There are various articles on the internet describing these, so you may want to do a search.

Railscasts - Free Ruby on Rails Screencasts