Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare type of instance variable on controller

I have a crystal-lang project on Amber framework with Jennifer.cr and I'm getting this error on my controller:

Can't infer the type of instance variable '@companies' of CompanyController
@companies = Company.all

The controller is:

class CompanyController < ApplicationController
  def index
    @companies = Company.all
    render("index.slang")
  end
end

When I try to solve the problem this way:

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

I got another error:

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

How can I solve this "easy" problem?

like image 560
Osmond Avatar asked Oct 24 '17 16:10

Osmond


People also ask

How do you set instance variables?

Instance variables can be declared at the class level before or after use. Access modifiers can be given for instance variables. The instance variables are visible for all methods, constructors, and block in the class. Normally, it is recommended to make these variables private (access level).

Is instance type variable?

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class.

How do you name an instance variable?

Instance variable names begin with an underscore ( _ ) followed by a camel case alphanumeric string. The first character following the underscore must be a lowercase letter; numerals are not permitted.

What is an instance variable in Rails?

A variable set in Rails controller starting with @ sign are called instance variables. The specialty of instance variables is that they're available in our Rails views. We don't need to pass them explicitly to our Rails views. That's why in the index.


2 Answers

You don't have to use instance variable here. Local variable is a way Amber app uses by default (and they are accessible in views):

class CompanyController < ApplicationController
  def index
    companies = Company.all
    render("index.slang")
  end
end

But if you want to use an instance variable due to some reason, you need to declare and initialize it at a class level or follow other type inference rules.

like image 78
Vitalii Elenhaupt Avatar answered Sep 19 '22 17:09

Vitalii Elenhaupt


As mentioned using a local variable here is the most elegant solution. For people ending up here with a similar error message but in a different context, read below:

The second error message already points to the right solution, the following code should work too:

class CompanyController < ApplicationController
  @companies : Array(Company)?

  def index
    @companies = Company.all
    render("index.slang")
  end
end
like image 40
Jonne Haß Avatar answered Sep 22 '22 17:09

Jonne Haß