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
end
end
To use that class I have the following code:
f = Request.new("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 = Request.new("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
start.
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.
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
"form_for"?
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:
index.html.erb:
<% form_tag(???, :method=>'post') do %> #1. I don't know
what I have to put in here. The corresponding method in my
controller???
Request:<br />
<%= text_field_tag "request" %>
<%= submit_tag 'Submit' %>
<% end %>
<%=@results%> #3.
Should be output the results...
request.rb:
class Request
def initialize(myrequest)
.....--> send myrequest via net/http to the web service
end
end
home_controller.rb:
class HomeController < ApplicationController #2. I don't
know how to receive the value from the form and send it to my model
def index
end
end
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: http://edgeguides.rubyonrails.org/getting_started.html#listing-all-posts . 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]
...so you can do:
my_request = Request.new(params[:request_to_run])
(use debugging to confirm all the params values, etc, as I'm writing
this off the top of my head!
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
Rails.
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
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.
XML:
<First_Layer>
<Second_Layer>
<Third_Layer>
Text that I want to get
</Third_Layer>
</Second_Layer>
</First_Layer>
hash:
{First_Layer => {Second_Layer => {Third_Layer => "Text that I want to
get"}}}
What is the easiest way to get out the information I want. Right now I
use the hash at use things like that: