Background
Melodiest is Sinatra application boilerplate. It provides generator and contains minimum configuration to develop application with Sinatra. The reason why I create Melodiest is because I want to create many small web application based on sinatra with other third party ruby gems as foundation. Originally, I wanted to name this project as Harmony or Melody, but because it was already taken I decided to name this project as Melodiest.
Installation
gem install melodiest
with Bundler, put this code in your Gemfile:
gem 'melodiest'
How to Use
generate app in current directory without database
melodiest -n my_app
generate app in target directory without database
melodiest -n my_app -t target/dir
generate app in current directory with database. -d option will generate app with Sequel orm and PostgreSQL adapter.
melodiest -n my_app -d
Example Usage
This example assume that PostgreSQL is already running and desired database is already exist. For complete example see github.com/kuntoaji/todo_melodiest
- run
melodiest -n my_app -d - cd
my_app - run
bundle install - create
config/database.ymland configure your database setting - create file
db/migrations/001_create_artists.rband put the following code:
Sequel.migration do
up do
create_table(:artists) do
primary_key :id
String :name, :null=>false
end
end
down do
drop_table(:artists)
end
end
- run
rake db:migrate - create file
app/models/Artist.rband put the following code:
class Artist < Sequel::Model
end
- create file
app/routes/artists.rband put the following code:
class MyApp
get '/artists' do
@artists = Artist.all
erb :"artists/index"
end
post '/artists' do
@artist = Artist.new
@artist.name = params[:name]
@artist.save
redirect '/artists'
end
end
- create file
app/views/artists/index.erband put the following code:
<h1>List of Artist</h1>
<ul>
<% @artists.each do |artist| %>
<li><%= artist.name %></li>
<% end %>
</ul>
<form action="/artists" method="post">
<%= Rack::Csrf.tag(env) %>
<input type="text" name="name" />
<button>Submit</button>
</form>
- run the server
bundle exec thin start - open url
localhost:3000/artists
List of Ruby Gems
- sinatra
- sinatra-contrib
- encrypted_cookie
Sinatra::Reloaderin development environment only- thin
Rack::Session::EncryptedCookieRack::Csrf- sequel
- sequel_pg as PostgreSQL adapter
- sinatra-asset-pipeline
- uglifier
- tux for console
Sinatra::Cachein production environment only