JtR
JtR

Reputation: 20676

How do I do html forms with sinatra?

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?

Upvotes: 21

Views: 19598

Answers (1)

jrom
jrom

Reputation: 1960

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

Upvotes: 44

Related Questions