DevHeads.net

Ruby & Forms

Hi folks.

I’m trying to get my head around the implementation of Ruby forms. This is not Rails. I’m messing around in Sinatra and wanted to know how to receive submitted forms. I’ve looked at CGI and the params tag, but I’m unclear as to which one should be used.

Any urls and insight appreciated as I can’t find much out there. I’m confused between the two applications and I can’t seem to get params to work properly.

Cheers

Comments

Re: Ruby & Forms

By sto.mar at 12/14/2014 - 17:13

Am 13.12.2014 um 17:45 schrieb Bee.Lists:
There's no such thing... you mean HTML forms.

Use the params hash to access the data, see the example app below.

There should be many examples online... for instance the Sinatra
documentation links to apps "In the Wild".

Below a minimal example, save it into a file and run it:

get "/" do
erb :index
end

post "/submit" do
@first_name = params[:first_name]

erb :hello
end

@@layout
<!DOCTYPE html>
<html>
<body>
<%= yield %>
</body>
</html>

@@index
<form action="/submit" method="post">
<label for="zahl">First Name:</label>
<input type="text" name="first_name" value="">
<input type="submit">
</form>

@@hello
<p>Hello, <%= @first_name %>!</p>

Best regards,
Marcus

Re: Ruby & Forms

By Bee.Lists at 12/15/2014 - 11:59

Hi Marcus.

When I say Ruby, I mean receiving the form submission. “params” was what I was looking for.

I will review your example. Thanks