1

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.

1 Answer 1

2

@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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.