dt1000
dt1000

Reputation: 3732

Group html elements in post - ruby with sinatra

I'm using Ruby with the Sinatra framework. I'm sorta new to html. I create a form, and when I look at my @params hash in post, it is a flat hash, as expected:

@params = { input_name_a => user_input_a, input_name_b => user_input_b}

Is there a way to set up some elements of the page to come back as groups, where a key points to an object of key value pairs (which are inputs), instead of a value? So my @params hash would look like this?

@params = { group_a => { input_name_a => user_input_a, input_name_b => user_input_b} }

thanks

Upvotes: 0

Views: 102

Answers (1)

dt1000
dt1000

Reputation: 3732

<form accept-charset="UTF-8" action="/clients" method="post">
  <input type="text" name="client[name]" value="Acme" />
  <input type="text" name="client[phone]" value="12345" />
  <input type="text" name="client[address][postcode]" value="12345" />
  <input type="text" name="client[address][city]" value="Carrot City" />
</form>

When this form is submitted, the value of params[:client] will be {"name" => “Acme”, “phone” => “12345”, “address” => {"postcode" => “12345”, “city” => “Carrot City”}}. Note the nested hash in params[:client][:address].

Upvotes: 0

Related Questions