Forum: Ruby on Rails Designing a data-entry screen

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris H. (Guest)
on 2005-12-20 06:36
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.
Chris H. (Guest)
on 2005-12-20 14:45
(Received via mailing list)
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])
%><br />
Month: <%= select_tag "month", options_for_select(@months,
@params[:month])
%><br />
Dept: <%= select "department", "id", @depts %><br />
Group: <%= select "group", "id", @groups %><br />
<%= submit_tag "Refresh" %><%= submit_tag "Save" %>
<%= end_form_tag %>

hope this helps
Chris H. (Guest)
on 2005-12-20 23:53
Thanks a lot! You're a champion.
This topic is locked and can not be replied to.