21

Is there some utilities available so that I could easily encapsulate form fields passed in requests in an object or do I have to create it myself by parsing fields from params in every request?

JtR
  • 20,568
  • 17
  • 46
  • 60

1 Answers1

44

Yes, since Sinatra 0.9 you can use Rails-like nested parameters:

You just declare your form as:

<form>
  <input ... name="post[title]" />
  <input ... name="post[body]" />
  <input ... name="post[author]" />
</form>

And then you just have to do:

@post = params[:post]

to fetch all the parameters in an object.

More information in Learn Ruby the Hard Way

Reuben Mallaby
  • 5,740
  • 4
  • 47
  • 45
jrom
  • 1,910
  • 1
  • 16
  • 12