rails_on_iis を Windows 10 で

# 概要

Ruby 素人が rails_on_iis を Windows 10 でやった際の、ハマり話です。

基本はこちら [Railsコラム – enjoy Railsway 第5回 「IIS 8+(HttpPlatformHandler)でRuby on Railsを実行」](https://www.transnet.ne.jp/2015/04/03/rails%E3%82%B3%E3%83%A9%E3%83%A0-coler-%E3%80%8Ciis-8httpplatformhandler%E3%81%A7ruby-on-rails/) を参照。

IIS 7 以上で Rails プログラムを構成する方法が 2 つあります。

## httpPlatformHandler を使用

Ruby 側で WEBrick などの web server を立ちあげて IIS の httpPlatformHandler でつなぐ方法です。この方法は汎用性が高いです。

![WEBrick.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364126/09132eed-8373-2e27-4fc8-b7897085d518.png)

## Helicon Zoo を使用

Helicon Tech 社の Helicon Zoo を使用する方法です。

https://www.codeproject.com/Tips/805628/Ruby-on-Rails-On-Windows-Server-With-Helicon-Zoo

https://www.helicontech.com/zoo/install.html

`C:\Zoo\Workers\ruby\zoorack.rb` は Helicon Zoo の一部です。

![Rack.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364126/4debbb1c-ebe6-ace4-f832-3161af48a591.png)

# ハマリポイント

## `C:\inetpub\wwwroot\rails_on_iis` へ書き込みできない場合

リクエストがクルクル回り続ける…

![2020-01-31_23h52_29.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364126/ea085b6e-72c5-7918-6448-3fe7863a6c4c.png)

イベントビューアの Application ログを確認:

![2020-01-31_23h53_35.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364126/40d25751-dc10-d2c2-0dcc-006d11121985.png)

`Warning: Could not create stdoutLogFile \\?\C:\inetpub\wwwroot\rails_on_iis\httpplatform-stdout_16724_2020131145254.log, ErrorCode = -2147024891.`

## ActionController::RoutingError エラー

`C:\inetpub\wwwroot\rails_on_iis\log\production.log` で、`ActionController::RoutingError (No route matches [GET] "/rails_on_iis/books"):` エラー

```
I, [2020-02-01T00:09:40.125383 #17132]  INFO -- : [06380107-4723-48b5-94ec-6bf5375491f4] Started GET "/rails_on_iis/books" for 127.0.0.1 at 2020-02-01 00:09:40 +0900
F, [2020-02-01T00:09:40.129327 #17132] FATAL -- : [06380107-4723-48b5-94ec-6bf5375491f4]   
[06380107-4723-48b5-94ec-6bf5375491f4] ActionController::RoutingError (No route matches [GET] "/rails_on_iis/books"):
[06380107-4723-48b5-94ec-6bf5375491f4]   
[06380107-4723-48b5-94ec-6bf5375491f4] actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] railties (6.0.2.1) lib/rails/rack/logger.rb:38:in `call_app'
[06380107-4723-48b5-94ec-6bf5375491f4] railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `block in call'
[06380107-4723-48b5-94ec-6bf5375491f4] activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
[06380107-4723-48b5-94ec-6bf5375491f4] activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
[06380107-4723-48b5-94ec-6bf5375491f4] activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `tagged'
[06380107-4723-48b5-94ec-6bf5375491f4] railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] actionpack (6.0.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] rack (2.1.2) lib/rack/method_override.rb:24:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] rack (2.1.2) lib/rack/runtime.rb:24:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] rack (2.1.2) lib/rack/sendfile.rb:113:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] actionpack (6.0.2.1) lib/action_dispatch/middleware/host_authorization.rb:77:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] railties (6.0.2.1) lib/rails/engine.rb:526:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] puma (4.3.1) lib/puma/configuration.rb:228:in `call'
[06380107-4723-48b5-94ec-6bf5375491f4] puma (4.3.1) lib/puma/server.rb:681:in `handle_request'
[06380107-4723-48b5-94ec-6bf5375491f4] puma (4.3.1) lib/puma/server.rb:472:in `process_client'
[06380107-4723-48b5-94ec-6bf5375491f4] puma (4.3.1) lib/puma/server.rb:328:in `block in run'
[06380107-4723-48b5-94ec-6bf5375491f4] puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
```

`C:\inetpub\wwwroot\rails_on_iis\config.ru` を修正。
参考にしました: [新しいHttpPlatformHandlerによる、IIS 8上でのRuby on Rails (またはそれ以外の何でも) の実行
](https://satonaoki.wordpress.com/2015/02/12/rails-iis-httpplatformhandler/)

```config.ru
# This file is used by Rack-based servers to start the application.

require_relative 'config/environment'

Rails.application.config.relative_url_root = '/rails_on_iis'

map Rails.application.config.relative_url_root || "/" do
  run Rails.application
end
```

## `SQLite3::SQLException: no such table: books`

エラー

```
I, [2020-02-01T00:12:02.038522 #10708]  INFO -- : [b67a6be8-15f8-48c0-9326-3be217b07b8f] Started GET "/rails_on_iis/books" for 127.0.0.1 at 2020-02-01 00:12:02 +0900
I, [2020-02-01T00:12:02.039721 #10708]  INFO -- : [b67a6be8-15f8-48c0-9326-3be217b07b8f] Processing by BooksController#index as HTML
I, [2020-02-01T00:12:02.041988 #10708]  INFO -- : [b67a6be8-15f8-48c0-9326-3be217b07b8f]   Rendering books/index.html.erb within layouts/application
D, [2020-02-01T00:12:02.043934 #10708] DEBUG -- : [b67a6be8-15f8-48c0-9326-3be217b07b8f]   [1m[36mBook Load (1.0ms)[0m  [1m[34mSELECT "books".* FROM "books"[0m
I, [2020-02-01T00:12:02.044362 #10708]  INFO -- : [b67a6be8-15f8-48c0-9326-3be217b07b8f]   Rendered books/index.html.erb within layouts/application (Duration: 2.0ms | Allocations: 261)
I, [2020-02-01T00:12:02.044930 #10708]  INFO -- : [b67a6be8-15f8-48c0-9326-3be217b07b8f] Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.0ms | Allocations: 1197)
F, [2020-02-01T00:12:02.047193 #10708] FATAL -- : [b67a6be8-15f8-48c0-9326-3be217b07b8f]   
[b67a6be8-15f8-48c0-9326-3be217b07b8f] ActionView::Template::Error (SQLite3::SQLException: no such table: books):
```

実行

```bat
C:\inetpub\wwwroot\rails_on_iis>rails db:migrate RAILS_ENV=production
== 20200131144720 CreateBooks: migrating ======================================
-- create_table(:books)
   -> 0.0016s
== 20200131144720 CreateBooks: migrated (0.0025s) =============================
```

## `ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.):`

エラー

```
I, [2020-02-01T00:14:11.424988 #10708]  INFO -- : [7e8d7009-f3ad-424f-9baf-4027d40c0a41] Started GET "/rails_on_iis/books" for 127.0.0.1 at 2020-02-01 00:14:11 +0900
I, [2020-02-01T00:14:11.426340 #10708]  INFO -- : [7e8d7009-f3ad-424f-9baf-4027d40c0a41] Processing by BooksController#index as HTML
I, [2020-02-01T00:14:11.428806 #10708]  INFO -- : [7e8d7009-f3ad-424f-9baf-4027d40c0a41]   Rendering books/index.html.erb within layouts/application
D, [2020-02-01T00:14:11.431783 #10708] DEBUG -- : [7e8d7009-f3ad-424f-9baf-4027d40c0a41]   [1m[36mBook Load (1.9ms)[0m  [1m[34mSELECT "books".* FROM "books"[0m
I, [2020-02-01T00:14:11.445103 #10708]  INFO -- : [7e8d7009-f3ad-424f-9baf-4027d40c0a41]   Rendered books/index.html.erb within layouts/application (Duration: 15.8ms | Allocations: 1113)
I, [2020-02-01T00:14:11.447056 #10708]  INFO -- : [7e8d7009-f3ad-424f-9baf-4027d40c0a41] Completed 500 Internal Server Error in 20ms (ActiveRecord: 3.0ms | Allocations: 2501)
F, [2020-02-01T00:14:11.447968 #10708] FATAL -- : [7e8d7009-f3ad-424f-9baf-4027d40c0a41]   
[7e8d7009-f3ad-424f-9baf-4027d40c0a41] ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.):
```

実行。
参考: [Rails5でnginx+pumaでproduction環境を構築する](http://blog.atwata.com/middle/2017/09/30/rails5-nginx-puma.html)

```bat
bundle exec rake assets:precompile RAILS_ENV=production
```