Return to Tech/other

Rails





Routing
Layout
Example Create application $ rails new railsapp -d sqlite3 --skip-test-unit Done in 8.20s. Webpacker successfully installed 🎉 🍰 $ grep '^gem' Gemfile gem 'rails', '~> 6.0.2', '>= 6.0.2.1' gem 'sqlite3', '~> 1.4' gem 'puma', '~> 4.1' gem 'sass-rails', '>= 6' gem 'webpacker', '~> 4.0' gem 'turbolinks', '~> 5' gem 'jbuilder', '~> 2.7' gem 'bcrypt', '~> 3.1.7' gem 'bootsnap', '>= 1.4.2', require: false gem 'rails-i18n' gem 'kaminari' gem 'date_validator' gem 'valid_email2' gem 'nokogiri' gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] check yarn $ yarn yarn install v1.22.0 [1/4] Resolving packages... success Already up-to-date. Done in 0.96s. $ bin/bundle $ grep -v '^#' config/database.yml default: &default adapter: sqlite3 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> timeout: 5000 development: <<: *default database: db/development.sqlite3 test: <<: *default database: db/test.sqlite3 production: <<: *default database: db/production.sqlite3 $ bin/rails db:create Created database 'db/development.sqlite3' Created database 'db/test.sqlite3' $ cat config/application.rb require_relative 'boot' require 'rails/all' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module RailsApp class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 6.0 # Settings in config/environments/* take precedence over those specified here. # Application configuration can go into files in config/intializers # -- all .rb files in that directory are automatically loaded after loading # the framework and any gems in your application. # added config.time_zone = 'Tokyo' config.i18n.load_path += Dir[Rails.root.join("config", "locales", "**", "*.{rb,yml}").to_s] config.i18n.default_locale = :ja config.generators do |g| g.skip_routes true g.helper false g.assets false g.test_framework :rspec g.controller_specs false g.view_spec false end end end Run $ bin/rails server --binding=192.168.1.2 --port=3001 or bin/rails s -b 192.168.1.2 -p 3001 => Booting Puma => Rails 6.0.2.1 application starting in development => Run `rails server --help` for more startup options Puma starting in single mode... * Version 4.3.3 (ruby 2.6.5-p114), codename: Mysterious Traveller * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://192.168.1.2:3001 Use Ctrl-C to stop
$ bin/rails g rspec:install Running via Spring preloader in process 6755 create .rspec create spec create spec/spec_helper.rb create spec/rails_helper.rb $ cat spec/rails_helper.rb # This file is copied to spec/ when you run 'rails generate rspec:install' require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end # in _spec.rb will both be required and run as specs, causing the specs to be # run twice. It is recommended that you do not name files matching this glob to # end with _spec.rb. You can configure this pattern with the --pattern # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. # # The following line is provided for convenience purposes. It has the downside # of increasing the boot-up time by auto-requiring all files in the support # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } # Checks for pending migrations and applies them before tests are run. # If you are not using ActiveRecord, you can remove these lines. begin ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = true # RSpec Rails can automatically mix in different behaviours to your tests # based on their file location, for example enabling you to call `get` and # `post` in specs under `spec/controllers`. # # You can disable this behaviour by removing the line below, and instead # explicitly tag your specs with their type, e.g.: # # RSpec.describe UsersController, :type => :controller do # # ... # end # # The different available types are documented in the features, such as in # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! # Filter lines from Rails gems in backtraces. config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") end new spec $ mkdir spec/test00 $ vi spec/test00/string_spec.rb $ cat spec/test00/string_spec.rb require "spec_helper" describe String do describe "#<<" do example "add" do s = "hogehoge" s << "fugafuga" expect(s.size).to eq(16) end end end $ rspec spec/test00/string_spec.rb . Finished in 0.00288 seconds (files took 0.13047 seconds to load) 1 example, 0 failures

Return to Tech/other