Domanda

I am building a Booking model in Rails 3.2.3 where a user steps through several form-screens of choices. If the form exactly mirrored the underlying model I know I could use a gem (e.g. Wicked gem) to build a multi-step form. The issue I am having is that on one of the form-screens there are multiple options for the user and within each of those options there are multiple options coming from an external web-service. In other words, on step 2 of the multi-step process, the form presents the user with many options of Rates from our database and for each Rate we have multiple additional options from the web-service. So a user would need to choose one of the options from the web-service (radio buttons) and then make a selection of their Rate of choice. This is then repeated multiple times on this page (although the user can only choose one radio-button option from the web-service and one Rate).

Where I am unsure of best practice is that I can display the multiple options from the web-service as radio buttons but there is a Hash of values associated with each of those options and hence with each of those radio buttons.

So, the question is, should I be attempting to pass that Hash as a param to the next step of the form process or should I be making that into an object and passing that or something else entirely!

I know this is a long explanation but I feel it's a critical point in the design of this workflow and I want to get it right.

Many thanks in advance,

J.

EDIT

Thinking it through again, the initial problem is how to represent a series of radio buttons when each radio button represents many values as opposed to say an id (in this instance each radio button represents a hash of values from the external web-service). Should the hash be made into an object and this passed instead - something along those lines?

È stato utile?

Soluzione

I figured this out. On inspecting the "hash" coming back from the external web-service, I noticed that date fields were not in quotes, e.g.

:departs=>Wed, 21 Aug 2013 10:40:00 +0000

whereas all the other fields were in quotes. This made me a little suspicious. So in the end I used to_json on the returned hash:

response_hash_from_webservice = data.to_json

In the form I then used:

JSON.parse(response_hash_from_webservice).each do |nested_item|
  # Access elements like so
  ... nested_item['company_name'] etc ...
end

However I needed to post this nested_item through to a next step of the form (as a radio button) and it only worked by again using to_json.

<%= radio_button_tag 'nested_item', nested_item.to_json %>    

I could then post this value or put it in the session and on the following form page use:

require 'json'
hash = JSON.parse session[:nested_item]

And then access the values as normal:

<%= nested_item['company_name'] %>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top