Skip to content

Instantly share code, notes, and snippets.

@wildmaples
Last active May 8, 2023 23:13
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save wildmaples/880c57c7beaa1dab7c44681279c66e6d to your computer and use it in GitHub Desktop.
Save wildmaples/880c57c7beaa1dab7c44681279c66e6d to your computer and use it in GitHub Desktop.
[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

Many thanks for this tutorial 👍

@Kepler-C
Copy link

Great article / tutorial, thank you very much!
One question though: Shouldn't be in the index.html.erb file, line 14
<%= text_field_tag("name") %>
and line 18
<%= date_field_tag("date", nil) %>
?

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