我有一个仅使用Rails5 api模式的应用程序。
我的config / application.rb看起来像这样:
添加了application.rb
module Myapi
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource (
'*',
headers: :any,
methods: [:get, :patch, :put, :delete, :post, :options]
)
end
end
end
end
但是我遇到了以下错误:
.gem/ruby/2.5.1/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in `require':
ndflo-api/goforfloapi/config/application.rb:38: syntax error, unexpected ',', expecting ')' (SyntaxError)
'*',
引起此语法错误的第38行的语法出了什么问题?
第38行如下:
'*',
答案 0 :(得分:1)
您收到该错误,是因为您添加了括号,删除了括号,并且没有在'*'
之后插入新行resource
,
应该是这样的:
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :patch, :put, :delete, :post, :options]
end
end
答案 1 :(得分:0)
语法在这里
这样做,经过测试。
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
methods: [:get, :patch, :put, :delete, :post, :options]
end
end
答案 2 :(得分:0)
我通过更改资源行使其变为单线来解决此问题,如下所示:
resource '*', headers: :any, methods: [:get, :patch, :put, :delete, :post, :options]