Jim's
Tutorials

Fall 2018
course
site

Rails Week 13

This week I still messed around with server stuff and learning how all of that works. I published my article on medium, Make A Simple MVC Sinatra App after finishing a second draft yesterday (thursday nov. 29th) and I'm actually pretty pleased with it. I think it clearly lays some of the ideas out and I think it was pretty cool to make some boilerplate people can download and follow along with.

...Jim, can you visit the article and give me applause please? LOL.

Anyway, in addition to this I explored 'posting' things and I played around with it in the todo list I made earlier in my explorations.

for example, in my index.rb file I added:

<form action= "/items" method= "post">
    <label for = "item_name">Additional Index Item:</label>
    <input type = "text" id= "item_name" name= "item_name">
    <input type = "submit">
</form>

(see picture 1 for result)... and then I played around with replacing 'text' with 'month' etc, and all the different options that I read about in this documentation. The results were pretty cool!

repeated for practice

I made a form on a browser that has a list and that adds your input to the list when you hit submit. I used ingredients.

server.rb

require 'sinatra'
require "pry" if development? || test?
require "sinatra/reloader" if development?
set :bind, '0.0.0.0'
get '/' do
  @ingredients = File.readlines("ingredients.txt")
  erb :index
end
get "/:whatever" do
    @ingredient_name = params[:whatever]
erb :show
end
post "/ingredients" do
  ingredient = params["ingredient_name"]
  File.open("ingredients.txt", 'a') do |file|
    file.puts(ingredient)
  end
  redirect '/'
end


ingredients.txt

1 1/2 pounds Brussels sprouts
3 tablespoons good olive oil
3/4 teaspoon kosher salt
1/2 teaspoon freshly ground black pepper

hi
hi
hi
hello

index.rb

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Roasted Brussels Sprouts</title>
  </head>
  <body>
    <h2>Roasted Brussels Sprouts</h2>
    <h3>Ingredients</h3>
    <ul>
      <% @ingredients.each do |ingredient| %>
        <li><%= ingredient %></li>
      <% end %>
    </ul>
    <form action= "/ingredients" method= "post">
    <label for = "ingredient_name">Additional Index Item:</label>
    <input type = "text" id= "ingredient_name" name= "ingredient_name">
    <input type = "submit">
</form>
  </body>
</html>

show.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Basic HTML Page</title>
    <link rel="stylesheet" href="/home.css">
  </head>

  <body>
    <h1>
      The item is <%= @ingredient %>
    </h1>

    <a href="/ingredients">All items</a>
  </body>
</html>

AND here's how the finished product looks...(see picture 1)

attachments [paper clip]

  last modified size
TXT Screen_Shot_2018-12-01_at_5.04.36_PM.png Wed Dec 04 2024 08:36 am 137K