Mark,
here is something I knocked up the other day. It allows navigation
and editing all on one screen, using ajax to do the updates and
navigate the select list. My plan is to integrate it into a redBox
popup for product categories. You may find some of it useful. The
select list part could be used in isolation for allocating the
category and the edit/add/delete fields included when editing
categories. this is still in early form, and there is room for
improvement. Perhaps it needs to be better crudded. But it works as
an example of a possible solution. Table has id, parent_id and name
fields. I havn''t added sort to it yet, but I probably will. For
clarity and brevity, I have removed the css formatting stuff that
makes it pretty, but you will get the idea.
Controller:
----------------------------
class CategoryController < ApplicationController
def show
@category=Category.find_by_parent_id nil
end
def pick
if params[:id]
@category=Category.find params[:id]
elsif params[:new]
@category=Category.find params[:category_id]
@new_category=@category.children.build :name=>params[:name]
@category.save
@category=@new_category # optional - to go to new category rather
than stay in current one
elsif params[:delete]
@category=Category.find params[:delete_id]
@parent=@category.parent
r=@category.destroy
@category=@parent if r
elsif params[:category_id]
@category=Category.find params[:category_id]
@category.update_attribute :name, params[:name]
else
@category=Category.find_by_parent_id(nil)
end
render :update do |page|
page.replace_html ''pick_category'',
:partial=>''pick''
page.replace_html :prompt,
:partial=>''layouts/all_messages''
end
end
end
------------------------
VIEWS:
show.rhtml
-------------------------
<div id=''pick_category'' style=''text-align:
left;''>
<%=render :partial=>''pick'' %>
</div>
---------------------
_pick.rhtml
--------------------
<label>Select Category:</label>
<%= category_select(@category)%> <br />
<%= observe_field ''category_select'',
:url=>{:action=>''pick''},
:with=>''id''
%>
<label >Edit Category Name: </label>
<%= text_field(:category, :name) if @category.ancestors.length>0
%><br />
<%= observe_field(''category_name'',
:url=>{:action=>''pick'',
:category_id=>@category},
:with=>''name'') if
@category.ancestors.length>0
%>
<label >New Sub Category: </label>
<%= text_field_tag(''new_category'') if
@category.ancestors.length<3 %>
<br />
<%= observe_field(''new_category'',
:url=>{:action=>''pick'',
:category_id=>@category, :new=>true},
:with=>''name'') if
@category.ancestors.length<3
%>
<label >Delete Category: </label>
<%link_to_remote(@category.name,
:url=>{:action=>''pick'', :delete=>true,
:delete_id=>@category})
if @category.ancestors.any? and !@category.children.any? %> <br />
---------------------------------
HELPERS:
def category_select(cat)
depth,options=build_ancestors(cat)
cat.children.each {|c| options<< ["..../"*depth+c.name,
c.id]}
select_tag ''category_select'', options_for_select(options,
cat.id)
end
def build_ancestors(cat)
if cat.parent_id
depth, options=build_ancestors(cat.parent)
return depth+1, options << ["..../"*depth+cat.name, cat.id]
else
return 0,[[cat.name, cat.id]]
end
end
------------------------------
Note 1. I particularly wanted to navigate the tree rather than
display the whole tree with all branches in the
select field so I have written the helpers to do that. You may prefer
to populate all branches, in which case there is example code on the
rails wiki.
Note 2. I don''t offer the delete option if the category has child
categories. You may prefer to allow deletion of the whole sub
branch.
hope this may be of some value
Tonypm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---