Forum: Ruby on Rails ordering of params

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Joe Van D. (Guest)
on 2006-02-02 19:13
(Received via mailing list)
I have a few long forms.  I want to email out the user's response on the

So I have something like:

# View
<%= text_field 'answer', 'name' %> Name <br />
<%= text_field 'answer', 'email' %> Email <br />

# Controller
def form_submit
  answers = params[:answers]
  email_text = "New form submission receieved"
  answers.each do |question, answer|
    email_text << "#{ question.humanize }: #{ answer }\n"
  # send email

However, I don't iterate through the answers in the order that they
were in the form, the answers are retrieved from params in a random
order (since hashes aren't ordered).

Is there any way to do something like this and keep a sensical order
to the email?

Gabe B. (Guest)
on 2006-02-02 22:30
(Received via mailing list)
I think this is kinda ugly, but it should do the trick:

QUESTION_ORDER = %w{ name email ... }

def form_submit
  answers = params[:answers].sort_by { |question, _|
question.to_s) }
  email_text = "New form submission received"
  answers.each do |question, answer|
    email_text << "#{ question.humanize }: #{ answer }\n"

This topic is locked and can not be replied to.