OK, I’ve got my code working. As you will no doubt infer from my
questions below, I am completely new to web development. If clueless
n00b questions drive you crazy, then hit now and move on…
For those of you I haven’t scared away yet…
I have a table listing document names and numbers. I have a view with
a “select” box allowing the user to narrow the list of documents down
to those that fall into certain categories (as defined by the document
number prefix in the table). I display the table of documents using
active scaffold embedded in by index view.
The problem is… the code looks (to my n00b eyes) to be very
inelegant. I was wondering how others solve this problem or, how you
would clean up this code to make it look less kludgy…
Here is a snippet from my controller…
=======================================
class DocumentsController < ApplicationController
CATEGORIES =
[{:id => 0, :text => ‘All’, :min=>0, :max => 99},
{:id => 1, :text => ‘Standard Operating Procedures’, :min=>0, :max
=>0},
{:id => 2, :text => ‘Functional Group Policies and Procedures’,
:min => 1, :max => 1},
{:id => 3, :text => ‘Project Level Documentation’, :min => 2, :max
=> 9},
{:id => 4, :text => ‘Software Documentation’, :min => 10, :max =>
19},
]
CATEGORIES_FOR_SELECT = CATEGORIES.collect {|c| [c[:text], c[:id]]}
active_scaffold :document do |config|
# …
end
GET /documents
GET /documents.xml
def index
@documents = Document.find(:all)
@category = params[:category].to_i
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @documents }
end
end
def set_category
redirect_to documents_path(:category => params[:category])
end
end
=====================================
and here is a snippet from my view:
<%= content_tag :h1, “Document Log” %>
<% content_tag :fieldset do -%>
<% form_tag url_for_options = {:action => :set_category} do -%>
<%= content_tag :legend, “Document Category” %>
<%= select_tag ‘category’,
options_for_select(DocumentsController::CATEGORIES_FOR_SELECT,
@category) %>
<%= submit_tag ‘Go’ %>
<% end -%>
<% end -%>
<% @category ||= 0 %>
<% min = DocumentsController::CATEGORIES[@category][:min] %>
<% max = DocumentsController::CATEGORIES[@category][:max] %>
To me, this looks a little kludgy (because it has been kludged
together 15 minutes at a time as I’ve been starting to learn RoR)
-
Is this how folks typically code select boxes with constant
content? Being a long time C programmer, I tend to think in terms of
“struct”. So I created a class level constant array,
DocumentsController::CATEGORIES containing :id, :text, :min, and :max
fields. From that array of structures (see, my “C” is coming out), I
derived a new class level constant array,
DocumentsController::CATEGORES_FOR_SELECT containing just the :text
and :id fields (in that order) which I could pass to
#options_for_select. -
It seems odd to me to invoke the #set_category action just to have
it redirect back to the index action. (This is my code review in
action here – I’m looking at the code now and saying, gee that could
probably have been made cleaner). Under RESTful routing, is there a
way I could have had the submit button point right back at
#documents_path with the :category parameter set? -
I think I am most dissatisfied with my view… It seems like a
lot of ERB code and very little HTML. Is that typical? What is the
more common practice in the Rails community – to use
<%= content_tag :h1, “Document Log” %>
or to use
Document Log
in the view?
-
What about the rest of the ERB code? Did I use the #form_tag and
#select_tag helper functions in the way that you (more experienced RoR
developers) would use them, for this simple example of creating a
select box of constant values? -
How about my ERB code that extracted “min” and “max” from
DocumentsController::CATEGORIES… is that the way you would do it? -
I implemented this for now as a “select” tag with a “Go” button
that results in a new URL with the category parameter tacked on…
documents?category=1. Ultimately, I would like to learn enough about
Ajax to update the view of the table dynamically and automatically
when a user selects a new category. If anybody can give me a pointer
on where to get started with that, I would appreciate it.
If you’ve read this far, thank you very much. I have trouble writing
short mailing list posts If you happen to have opinions on and/or
answers to my questions and reply, thank you even more!
–wpd