Wednesday, August 15, 2012

RoR to Sinatra



I mentioned in an earlier post that I was going to finish a Ruby on Rails Tutorial. This way, I can get paid helping clients from odesk as I expand my knowledge about ruby web frameworks.

While reading Agile Web Dev with Ruby, I had an insight that I can apply what I am learning about Ruby on Rails to Sinatra (killing two birds with one ruby). It sounded plausible (as Mythbusters would say)... I'd just look up the Sinatra and HAML references to see what changes in the code that I had to make.

On the tutorial, the author showed how to insert server time into the index page. He did this by inserting ruby code on the say_controller.rb file
def hello
  @time = Time.now
end

...then, opening hello.html.erb inserted @time.
<h1>Hello from Rails!<⁄h1>
It is now <%= @time %>

I played around with this on my sinatra app, and after about an hour, here's how I did it.

Web.rb is my controller file. I inserted a function at the bottom...
def time
  Time.now
end

Opening views/index.haml, I inserted ruby code using haml...
%p
  This website runs on Sinatra (a Ruby web framework) and HAML.
%p
  It's going to be some sort of blog that I am going to update on a regular basis. Stay tuned!
%p
  It is now 
  = time

I learned that, to insert ruby code using haml, first you type the equal sign, followed by the ruby code. In this case, = time, where time is the function that I def'd in web.rb which gives us Time.now.

No comments:

Post a Comment