cluster1
cluster1

Reputation: 5674

Rails: Do which instance belongs a variable, which is defined in a controller?

If I define an instance-variable in a Rails-controller like for example:

def new
    @article = Article.new
end

I know that I declare an instance-attribute there. But: To which instance will @article become a member of?

How is it possible that it is accessible afterward in the according view without specifying an instance?

For example:

<%= @article.description %>

Actually I would expect something like myInstance.article.description.

Upvotes: 1

Views: 35

Answers (1)

rmlockerd
rmlockerd

Reputation: 4126

@article is an instance variable of your controller class (presumably ArticlesController given your code snippet). When your controller renders a view, the Rails replicates the controller's instance variables automatically so that the view gains the same instance variables.

Upvotes: 2

Related Questions