Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby + Cuba + Ohm

Ruby + Cuba + Ohm

Cómo armar tu primer web app con Ruby sin rails, sin saber Ruby ni rails. Usando Cuba y Ohm con Redis.

Demo: https://github.com/kevinjhanna/barcampba12

BarCamp Buenos Aires 2012

Kevin J. Hanna

November 04, 2012
Tweet

Other Decks in Programming

Transcript

  1. Cómo armar tu primer web app con Ruby sin rails,

    sin saber Ruby ni rails. Usando Cuba y Ohm con Redis.
  2. Ruby > ["foo", "bar"] => ["foo", "bar"] > hash =

    {foo: "bar"} => {:foo=>"bar"} > hash[:foo] => "bar"
  3. > name = "kevin" => "kevin" > "mi nombre es

    #{name}" => "mi nombre es kevin"
  4. # cat app.rb require "cuba" Cuba.define do on root do

    res.write "Hola, barcamp!" end end
  5. # cat app.rb require "cuba" Cuba.define do on root do

    res.write "Hola, barcamp!" end end
  6. # cat app.rb Cuba.define do on root do res.write(view("home")) end

    end # cat views/home.erb <h1>Charlas del BarCamp</h1>
  7. # cat app.rb Cuba.define do on root do res.write(view("home", place:

    "sala")) end end # cat views/home.erb <h1>Charlas del BarCamp</h1>
  8. # cat app.rb Cuba.define do on root do res.write(view("home", place:

    "sala")) end end # cat views/home.erb <h1>Charlas del BarCamp</h1> <p>En <%= place %></p>
  9. talk = Talk.create( title: "Cómo armar un app en Ruby",

    description: "Es muy divertida", author:"Kevin J. Hanna" ) talk.id # => 1 talk.title # => "Cómo armar un app en Ruby" talk == Talk[1] # => true
  10. Cuba.define do on "charlas/crear" do on get do res.write(view("create")) end

    end end <h1>Subí tu charla de barcamp</h1> <form method=post action="/charlas/crear"> <input type=text name=title> <input type=text name=author> <textarea name=description></textarea> <button type=submit>Crear</button> </form>
  11. Cuba.define do on "charlas/crear" do on get do res.write(view("create")) end

    on post do talk = Talk.create( title: req.POST["title"], author: req.POST["author"], description: req.POST["description"], ) res.redirect("/charlas/#{talk.id}", 302) end end end
  12. Cuba.define do on "charlas/crear" do on get do res.write(view("create")) end

    on post do talk = Talk.create( title: req.POST["title"], author: req.POST["author"], description: req.POST["description"], ) res.redirect("/charlas/#{talk.id}", 302) end end on get, "charlas/:id" do |id| talk = Talk[id] res.write(view("talk", talk: talk)) end end
  13. Cuba.define do ... on root do res.write(view("home", talks: Talk.all)) end

    end <% talks.each do |talk| %> <p><%= talk.title %></p> <% end %>
  14. # cat lib/talk.rb class Talk < Ohm::Model attribute :title attribute

    :description attribute :author collection :comments, :Comment end
  15. Comment.create( talk_id: 1, body: "bueno", rating: 3 ) Comment[1].talk.title #

    => "Cómo armar un app en Ruby" Talk[1].comments # => [Comment[1]] * Comment[1].body # => "bueno"
  16. <header> <h1><%= talk.title %></h1> </header> <p>Autor: <%= talk.author %></p> <p><%=

    talk.description %></p> <h2>Escribir comentario</h2> <form method=post action="<%= "/charlas/#{talk.id}" %>"> <textarea name=body></textarea> <input type=text name=rating></input> <button type=submit>Comentar</button> </form>
  17. on "charlas/:id" do |id| on get do res.write(view("talk", talk: Talk[id]))

    end on post do Comment.create( rating: req.POST["rating"], body: req.POST["body"], talk_id: id, ) res.redirect("/charlas/#{id}", 302) end end
  18. <header> <h1><%= talk.title %></h1> </header> <p>Autor: <%= talk.author %></p> <p><%=

    talk.description %></p> <h2>Escribir comentario</h2> <form method=post action="<%= "/charlas/#{talk.id}" %>"> <textarea name=body></textarea> <input type=text name=rating></input> <button type=submit>Comentar</button> </form>
  19. <header> <h1><%= talk.title %></h1> </header> <p>Autor: <%= talk.author %></p> <p><%=

    talk.description %></p> <h2>Escribir comentario</h2> <form method=post action="<%= "/charlas/#{talk.id}" %>"> <textarea name=body></textarea> <input type=text name=rating></input> <button type=submit>Comentar</button> </form> <% talk.comments.each do |comment| %> <p><%= comment.rating %></p> <% end %>