Skip to content

Instantly share code, notes, and snippets.

@wildmaples
Last active Dec 30, 2021
Embed
What would you like to do?
[Mirth Final] Web application using Rails libraries
<%# Place this file within under daily_steps/ directory %>
<%= tag.ul do %>
<% @all_birthdays.each do |birthday| %>
<%= tag.li do %>
<%= tag.b(birthday.name) %> was born on <%= birthday.date %>!
<% end %>
<% end %>
<% end %>
<%= form_tag(birthdays_path) do %>
<%= tag.p do %>
<%= label_tag("text", "Name") %>
<%= date_field_tag("name") %>
<% end %>
<%= tag.p do %>
<%= label_tag("date", "Date") %>
<%= number_field_tag("date") %>
<% end %>
<%= button_tag("Submit birthday") %>
<% end %>
# mirth-final.rb
# Replace code with Rails library
require 'rack'
require 'rack/handler/puma'
# Add the Rails libraries we need
require 'action_controller'
require 'active_record'
require 'action_dispatch'
# Create a connection between AR and the database
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "mirth.sqlite3")
# Create a AR model for the birthdays
class Birthday < ActiveRecord::Base; end
# Ensure the Action Controller reads views from root
ActionController::Base.prepend_view_path(".")
# Create a router to manage endpoints
router = ActionDispatch::Routing::RouteSet.new
# Create a controller for birthdays
# Each action represents an endpoint
class BirthdaysController < ActionController::Base
def index
@all_birthdays = Birthday.all
end
def create
Birthday.create(name: params['name'], date: params["date"])
redirect_to(birthdays_path, status: :see_other)
end
def all_paths
render(plain: "✅ Received a #{request.request_method} request to #{request.path}!")
end
end
# Create router to manage endpoints
router = ActionDispatch::Routing::RouteSet.new
# Include url helpers module to use `birthdays_path`
BirthdaysController.include(router.url_helpers)
router.draw do
# Creates standardised CRUD routes mapped to the controller
resources :birthdays
# Routes all paths to `birthdays#all_paths` action method
match '*path', via: :all, to: 'birthdays#all_paths'
end
Rack::Handler::Puma.run(router, :Port => 1337, :Verbose => true)
@taufikobet
Copy link

taufikobet commented Mar 31, 2021

Many thanks for this tutorial 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment