小编Osm*_*ond的帖子

在控制器上声明实例变量的类型

我和Jennifer.cr在Amber框架上有一个水晶项目,我在我的控制器上收到了这个错误:

Can't infer the type of instance variable '@companies' of CompanyController
@companies = Company.all
Run Code Online (Sandbox Code Playgroud)

控制器是:

class CompanyController < ApplicationController
  def index
    @companies = Company.all
    render("index.slang")
  end
end
Run Code Online (Sandbox Code Playgroud)

当我尝试以这种方式解决问题时:

class CompanyController < ApplicationController
  def index
    @companies : Array(Company) = Company.all
    render("index.slang")
  end
end
Run Code Online (Sandbox Code Playgroud)

我有另一个错误:

instantiating 'CompanyController#index()'
in src/controllers/company_controller.cr:7: declaring the type of an instance variable must be done at the class level

    @companies : Array(Company) = Company.all
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个"简单"问题?

crystal-lang amber-framework

8
推荐指数
1
解决办法
375
查看次数

如何检查Crystal中是否定义了常量

我需要验证是否定义了常量来执行条件.

我试过这个,但这种语言不存在"定义"方法:

if defined(constant)
  value = :foo
else
  value = :bar
end
Run Code Online (Sandbox Code Playgroud)

metaprogramming crystal-lang

6
推荐指数
1
解决办法
342
查看次数