3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails6でapiを書くときのcorsの設定での躓きについて

Posted at

この記事で書くこと

以下のようなアプリケーションを作ろうとしたときに、CORSの設定で本番反映後にエラーが発生してしまったので、同じミスをしないように情報をきちんとまとめておく。

Component 1.png

結論

firebaseでホスティングされたnuxtアプリケーションのurlが、 https://xxxx.web.app だった場合、

config/environments/production.rb
Rails.application.configure do
  # 中略
  config.hosts << 'xxxx.web.app'
end
config/initializer/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    if Rails.env.production?
      origins 'https://xxxx.web.app'
    else
      origins 'http://localhost:3000'
    end
    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

何が起きたか

Access to XMLHttpRequest at 'API側のurl' from origin 'アプリ側のurl' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

apiへリクエストがエラーになってしまう。 :cry:

うまく行ってなかったときの状況

Ruby: 2.7.1
Rails: 6.0.3.3
rack-cors: 1.1.1

rack-corsの設定

config/initializer/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    if Rails.env.production?
      origins 'https://xxxx.web.app/'
    else
      origins 'http://localhost:3000'
    end
    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

config/environments/production.rb

Railsガイド

config/environments/production.rb
Rails.application.configure do
  # 中略
  config.hosts << 'https://xxxx.web.app'
end

なぜ???間違ってないはず。。。
となったが、firebaseの
Hosting URL: https://xxxx.web.app という記述を見直していると最後の /がないのでは!????と気づく。

config/initializer/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    if Rails.env.production?
      origins 'https://xxxx.web.app' # 最後の/を削除する
    else
      origins 'http://localhost:3000'
    end
    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end
Access to XMLHttpRequest at 'API側のurl' from origin 'アプリ側のurl' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

そうすると、corsのエラーがなくなった。 :tada:

戦いはまだ終わってなかった。

次は、apiがstatus403で返ってきた。

config/environments/production.rb
config.hosts << 'xxxx.web.app'

「こうだよ!!」とresponseで親切に教えてくれていました。。。 ありがとう、、
:pray:

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?