【问题标题】:application.rb syntax error, unexpected ',', expecting ')'application.rb 语法错误,意外 ',',期待 ')'
【发布时间】:2018-07-11 06:28:02
【问题描述】:

我有一个 rails5 api-only 模式应用程序。

我的 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 行如下所示:

'*', 

【问题讨论】:

  • 你有什么问题?
  • 更新添加问题。
  • 也许在你的电话周围放置明确的() 看看它是否仍然存在?可能的口译员感到困惑?我个人没有看到任何语法错误。
  • 空白在 Ruby 中(有时)很重要。特别是m(a, b)m (a, b) 是不同的:第一个括号是方法调用括号,第二个被解释为表达式分组括号。并且 Ruby 没有逗号运算符,所以 m (a, b) 是一个语法错误。你想说resource(,而不是resource (

标签: ruby-on-rails ruby ruby-on-rails-5


【解决方案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

【讨论】:

  • 或者,保留括号,但在打开之前不要添加空格。
【解决方案2】:

这里的语法错误

这样做,经过测试。

config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*',
      headers: :any,
      methods: [:get, :patch, :put, :delete, :post, :options]
  end
end

【讨论】:

    【解决方案3】:

    我通过更改资源行使其成为单行来解决此问题,如下所示:

    resource '*', headers: :any, methods: [:get, :patch, :put, :delete, :post, :options]
    

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2019-12-06
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 2013-11-11
      • 1970-01-01
      相关资源
      最近更新 更多