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

5cca0e2c073c82240a361b7d870c08b2?s=128

Kevin J. Hanna

November 04, 2012
Tweet

Transcript

  1. 1.

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

    sin saber Ruby ni rails. Usando Cuba y Ohm con Redis.
  2. 3.
  3. 4.

    Ruby > ["foo", "bar"] => ["foo", "bar"] > hash =

    {foo: "bar"} => {:foo=>"bar"} > hash[:foo] => "bar"
  4. 5.

    > name = "kevin" => "kevin" > "mi nombre es

    #{name}" => "mi nombre es kevin"
  5. 8.

    # cat app.rb require "cuba" Cuba.define do on root do

    res.write "Hola, barcamp!" end end
  6. 11.

    # cat app.rb require "cuba" Cuba.define do on root do

    res.write "Hola, barcamp!" end end
  7. 20.
  8. 21.

    # cat app.rb Cuba.define do on root do res.write(view("home")) end

    end # cat views/home.erb <h1>Charlas del BarCamp</h1>
  9. 22.

    # 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>
  10. 23.

    # 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>
  11. 24.
  12. 28.
  13. 31.

    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
  14. 36.

    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>
  15. 38.

    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
  16. 39.

    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
  17. 41.

    Cuba.define do ... on root do res.write(view("home", talks: Talk.all)) end

    end <% talks.each do |talk| %> <p><%= talk.title %></p> <% end %>
  18. 43.

    # cat lib/talk.rb class Talk < Ohm::Model attribute :title attribute

    :description attribute :author collection :comments, :Comment end
  19. 45.

    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"
  20. 46.

    <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>
  21. 49.

    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
  22. 50.

    <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>
  23. 51.

    <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 %>
  24. 52.
  25. 53.
  26. 55.