Designing a data-entry screen


#1

Here’s what i’m trying to do:

Have 3 dropdowns up the top of a page, being: year, month, department,
and group. Also a button underneath that called refresh.
When you click refresh, it finds the 1 row of data from the database for
that year/month/dept/group and populates the fields into a set of
textboxes below.
Then you can edit what is in those textboxes, and click a ‘save’ button,
and it’ll update the record back into the database.

Here’s what i’m struggling getting to work:

How do you get the dropdowns up the top to keep their values after the
postback?
How do you access the values in those dropdowns from the controller?
How do you get the controller to perform different actions when they
click ‘refresh’ versus clicking ‘save’ ?

I’m a bit of a beginner, but i believe rails stands a good chance of
revolutionising my productivity, eventually when i get the hang of it! I
already know how to do all this in asp.net but i’m keen to learn rails.


#2

i might try something like this

(untested)

controller:

def do_form
if @request.method == “post”
# load the department and group from the values submitted
# you will need them to be able to have the values selected
# when you redisplay the form
@department = Department.find(@params[:department][:id])
@group = Group.find(@params[:group][:id])
if @params[:commit] == “Refresh”
# do your refresh stuff here
elsif @params[:commit] == “Save”
# do your save here
end
end

data to populate select tags

@years = (1970…2005).to_a # @ years = [1970, 1971, 1972, … 2005]
@months = (1…12).to_a # @months = [1, 2, 3, … 12]
@depts = Department.find(:all).collect { |d| [d.name, d.id] } # @depts

[[“dept 1”, 1], … ]
@groups = Group.find(:all).collect { |g| [g.name, g.id] } # #groups =
[[“group 1”, 1], … ]
end

do_form.rhtml:

<%= start_form_tag :action => :do_form %>
Year: <%= select_tag “year”, options_for_select(@dates, @params[:year])
%>

Month: <%= select_tag “month”, options_for_select(@months,
@params[:month])
%>

Dept: <%= select “department”, “id”, @depts %>

Group: <%= select “group”, “id”, @groups %>

<%= submit_tag “Refresh” %><%= submit_tag “Save” %>
<%= end_form_tag %>

hope this helps


#3

Thanks a lot! You’re a champion.