How can i get the value from a simple html form using ruby?

It’s running ~/public_html/runwebrick.rb=>
#!/home/user1/.rvm/rubies/ruby-1.9.3-p194/bin/ruby

require ‘webrick’

root = File.expand_path ‘~/public_html’
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
trap ‘INT’ do server.shutdown end
server.start

~/public_html/form.html=>

Username:

~/public_html/process.rb=>
#!/home/user1/.rvm/rubies/ruby-1.9.3-p194/bin/ruby

require “uri”
require “net/http”

#params = {‘name_input_username’ => ‘hello-world’,‘name_submit’ =>
‘Submit’}
x = Net::HTTP.post_form(URI.parse(‘http://localhost:8000/form.html’))
puts x.body

I got error for that=>
Method Not Allowed
unsupported method `POST’.
WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20) at localhost:8000

Can anyone post complete code to get the value from html form post?
Any answer is highly appreciated.
Thanks in advance.

As we tried to tell you in IRC, you are making this way too hard.

In PHP, you are already in the framework, in ruby, you have a choice
of frameworks that make getting form parameters from within your
application as easy as the php you quoted earlier: $_POST[‘bleh’].

The way to do this is to choose a framework and follow the directions on
that framework. In rails and sinatra, you use the params hash, ie.
params[:bleh]. This is as simple as it gets. There is no simpler way to
do this.

I am not going to write your code for you in the painful manner you are
attempting above.

Read the sinatra quick start guide, and the myriad examples of small
sinatra applications in the wild “example sintra application” should be
enough of a google search to get you going there.

Once you have the background knowledge to ask good questions, rather
than having someone write your code for you, you should come back to IRC
and ask away.

To reiterate what was said on IRC:

  1. If you want it to be as easy as PHP, use a framework such as sinatra.

  2. If you insist on doing it the old, outdated and hard way via CGI, you
    really do need to read a ruby book, the CGI rfc, the HTTP rfc and
    actually understand how these things work.

That is it.

–Christopher

What does cinatra or other framework do to get the value in this format?
puts params[:fname]

How is it done by those frameworks? What do they manage that to get the
value from form post?

Ah! To the meat of the problem.

Sinatra is built on rack, rack is a public project on github, read the
rack source, and you will have your answer.

Ultimately, it all boils down to text messages being passed across the
web.
Those requests are in a certain format (HTTP) that the servers then
parse
to extract the relevant information.

For example, start up irb and run:

require ‘socket’
server = TCPServer.new 2000
client = server.accept

Now go in a new tab and run (assuming you’re on linux)
$ curl http://localhost:2000/abc?def=hijkl

Then back to the irb console, and type client.gets until you see the
“\r\n” message (if you go too far, ctrl-C)

client.gets
=> “GET /abc?def=hijkl HTTP/1.1\r\n”

client.gets
=> “User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4
OpenSSL/0.9.8r zlib/1.2.5\r\n”

client.gets
=> “Host: localhost:2000\r\n”

client.gets
=> “Accept: /\r\n”

client.gets
=> “\r\n”

You can see the data there (this is a GET request, you can see by the
first
word of the first line). After this last “\r\n” is the body, which we
don’t
have since we made a GET request (if you posted a form, you would have
more
content here).

Now write some stuff back:

client.puts “hello”
client.puts “there”
client.puts “curl!”

And you’ll see it come out in the curl output.
Then run

client.close

and the curl session will end.

Real http responses, of course, are formatted as well, and consumers
like
curl and web browsers will parse and interpret them, but this should
show
you how they actually talk to each other.

(If you don’t have curl, you can make this request in a web browser)

-Josh