Rails application in one file
1ファイルでミニマムなRailsアプリケーションを記述する 実際の処理をすべてconfig.ruに書き、bundle exec puma -p 3000で実行可能なアプリケーションの例
code:Gemfile
gem 'rails'
gem 'puma'
code:ruby
require 'action_controller/railtie'
class MyApp < Rails::Application
config.eager_load = true # necessary to silence warning
config.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
config.secret_key_base = SecureRandom.uuid # Rails won't start without this
routes.append { root :to => "hello#index" }
end
class HelloController < ActionController::Base
def index
render json: { message: 'hello' }
end
end
MyApp.initialize!
run MyApp
2022年版
code:ruby
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
gem 'rails', '~> 7.0.4'
gem 'sqlite3'
end
require 'rails/all'
database = 'development.sqlite3'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: database)
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class App < Rails::Application
config.root = __dir__
config.consider_all_requests_local = true
config.secret_key_base = 'i_am_a_secret'
config.active_storage.service_configurations = { 'local' => { 'service' => 'Disk', 'root' => './storage' } }
routes.append do
root to: 'welcome#index'
end
end
class WelcomeController < ActionController::Base
def index
render inline: 'Hi!'
end
end
App.initialize!
run App