代码之家  ›  专栏  ›  技术社区  ›  Arunabh Das

application.rb语法错误,意外的“,”,应为“)”

  •  -4
  • Arunabh Das  · 技术社区  · 6 年前

    我有一个Rails5API模式应用程序。

    my 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行如下:

    '*', 
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   oj5th    6 年前

    因为添加了括号,所以出现了该错误,请删除括号,不要放置新行 '*' 之后 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
  •  0
  •   Nithin    6 年前

    这里的语法不对

    做这个,测试。

    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*',
          headers: :any,
          methods: [:get, :patch, :put, :delete, :post, :options]
      end
    end
    
        3
  •  0
  •   Arunabh Das    6 年前

    我修改了资源行,使其成为一个单独的行,如下所示:

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