I have a few long forms. I want to email out the user''s response on the form. 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" end # send email end 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? Thanks, Joe
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_ORDER.index( question.to_s) } email_text = "New form submission received" answers.each do |question, answer| email_text << "#{ question.humanize }: #{ answer }\n" end end Gabe On 2/2/06, Joe Van Dyk <joevandyk@gmail.com> wrote:> > I have a few long forms. I want to email out the user''s response on the > form. > > 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" > end > # send email > end > > 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? > > Thanks, > Joe > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: wrath.rubyonrails.org/pipermail/rails/attachments/20060202/fbd7acbb/attachment-0001.html