Using Rails for consuming a web service


I wrote pure Ruby code for consuming an existing web service. The
requests are send via net/http and are in XML format. That means I
have Ruby classes that look like this:

class Request
  def initialize(myrequest)
    .....--> send myrequest via net/http to the web service

To use that class I have the following code:

f ="This is a request").xml
p f

Now I want to make a rails web form where I can type my request and
send that to my existing classes to perform the request. All I found
via search were forms that worked with databases. I don't need a
database for that functionality. (I will need it later, when I store
the results, but that comes later!)

I already created a rails project and put my ruby file in the lib
folder. In my controller I have the following:

      f ="This is a request").xml
      @out = p f

So I am able to get the result of that request in my view via <%=@out
%>, but that is static.

How can I send "This is a request" via a web form from my view to that
ruby file?

I have no clue how to start. Again everything I find about rails is
database stuff.

I really hope that someone can help me here and give a clue how I can


If I understand you correctly, you are looking to submit a form to a rails controller that will then access your Request model and return you the results. It’s pretty easy if that is what you are looking for. Take your class Request code and save it in the model folder as is naming the file request.rb. Then generate a controller that you want to call with the form that will process the form and display requests. You can use all the tutorial stuff you found already. You’re just using a model that is not connected to a database. The key here is that your model request.rb is not inheriting from ActiveRecord::Base. It’s just a stand alone class file that you call like any other model.


Yes that is exactly what I want.

OK , I copied my file in the models folder and I guess I need to
create the form in my view with a "form_tag", or? Or do I need the

My problem is that I don't really know what I have to put in the
controller and my view (form) to call my model?

Can you give me a little example?

Sorry I am a real newbie to Rails. I am only good in Ruby.

What I have so far:


<% form_tag(???, :method=>'post') do %> #1. I don't know
what I have to put in here. The corresponding method in my
   Request:<br />
   <%= text_field_tag "request" %>
   <%= submit_tag 'Submit' %>
<% end %>

<%=@results%> #3.
Should be output the results...


class Request
  def initialize(myrequest)
    .....--> send myrequest via net/http to the web service


class HomeController < ApplicationController #2. I don't
know how to receive the value from the form and send it to my model
  def index

You are definitely on the right track. Knowing Ruby before you learn Rails will give you a huge leg up when creating applications on Rails. Picking up the framework is easy and you’ll get the hang of it soon enough.

For what you are trying to do you should not use index as that is mostly just for showing what you already know. You can change that behavior in the routing file but that is a more advanced topic for another day. To get you to where you want to be, the simplest example for handling a form is in section 6.7 (and up) from this tutorial: . When looking at it, think of the Post model as your own Request model. They are the same except you are not using a database. So where you see Post.all or Post.find just know that if you want to use those methods you would have to write them yourself in your model or hook your model to a db through ActiveRecord.


You don't *need* anything in your form.. it could just be a plain ol'
HTML form submitting to the URI of your controller.
In the controller, the params hash will have all the values from the
submitted form - so if you have a text input field named
"request_to_run", you'll have a params value:
  params[:request_to_run] you can do:

my_request =[:request_to_run])

(use debugging to confirm all the params values, etc, as I'm writing
this off the top of my head! :slight_smile:

In the controller action, populate "@results" with the results of your
model (BTW, I wouldn't call it "Request"... that's a reserved word in

@results = my_request.groovy_method_that_does_some_work

in your view you can conditionally draw it:

<%=h @results.inspect if @results %>

On the whole though, if this is all you want your Rails app to do,
then Rails is a bit of overkill. You'd (with your pure Ruby
experience) probably find Sinatra makes more sense for such a
requirement. Either way, Rails will do it too - it just loads up loads
of other functionality you're never going to use :slight_smile:

Thank you very much you both:

My form is now working and looking like this:

<form method="get" >
  Value1: <%= text_field_tag 'Value1', params[:value1] %>
  Value2: <%= text_field_tag 'Value2', params[:value2] %>
  Value3: <%= text_field_tag 'Value3', params[:value3] %>
<%= submit_tag "Submit" %>

I followed the advice from Micheal and used pure html form. Rails form
is also working if I use this:

<% form_tag nil, :method => :get do %>
  Value1: <%= text_field_tag 'Value1', params[:value1] %>
  Value2: <%= text_field_tag 'Value2', params[:value2] %>
  Value3: <%= text_field_tag 'Value3', params[:value3] %>
<% end %>

Is there any disadvantage to use pure html or rails???

My controller is looking this:

  def index
    @var1 =[:value1], params[:value1],
params[:value1]) if params[:value1]

So finally my controller is using the Mywebservice Model and save it
to the @var1, which can be read in my view.

I don't know if my code is beautiful, but it is working!!


My controller is looking this:

def index
@var1 =[:value1], params[:value1],
params[:value1]) if params[:value1]

Don't you mean :
@var1 =[:value1], params[:value2],
params[:value3]) if params[:value1] && params[:value2] && params[:value3]

In your code, you're not access value2 or value3...

I don't know if my code is beautiful, but it is working!!

It may not be beautiful, but working is the important thing - beauty
is a bonus! :slight_smile:

Yes you are right I forgot to check if the other values are not nil.

Thank you!

Just another question with my webservice. I am using Savon for my SOAP
requests and the output is a XML or a very big hash made out of the
XML. That means it is a XML or hash with a lot of different tags and
it is getting very deep, if that is the right word.


       Text that I want to get


{First_Layer => {Second_Layer => {Third_Layer => "Text that I want to

What is the easiest way to get out the information I want. Right now I
use the hash at use things like that:

puts resultarray[":First_Layer"][":Second_Layer"]["Third_Layer"]

That means a lot of code.

Thanks in advance