Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why do I hear that it is not good to share instance variables between controllers and views. I kind of like it because I can see immediately via the @ that something is coming from the controller. I see a nice way to use helpers in controllers here: http://www.stephencelis.com/2008/09/06/rails-controllers-views-and-variables.html. But I am using cancan for that portion.

share|improve this question
up vote 5 down vote accepted

The idea was put up in rails best practices: Replace instance variable with local variable

Basically if you don't use many partials or have a single plain view for each controller method just using the instance variables in the view wouldn't really cause any trouble.

Nevertheless, if you use many partials and therefore have many instance variables declared in your controller..., it might be less confusing if you use the alternative of passing the controller (instance)-variable to the partials explicitly. That way - while working in the partial VIEW - you have one explicit reference to the variable/object you want to work with that is not shared with 'all' partials that are included in the creation of the page.

After all, you can handle this just as you like to handle it, noone will tell you to do it otherwise. My personal view, however, is that I like the idea of having explicitly references in my partials/views that can not be confused with other variables.

share|improve this answer

You can get the analogy from global variables or variables passed by reference, and think of partials as functions. So using instance variable is pretty much like variable passed to function as reference with all it's pro's and con's:

# controller 
def index
  @var = 1
end

#view index.html.erb 
<%= @var %><br/>
<%= render 'foo' %><br/>
<%= @var %>

#partial _foo.html.erb
<%= @var = 2 %>

result will be

1
2
2

You may want such behavior, but in most cases you would not need it. And it's easier to introduce bugs this way and harder to track them. That's why this is recommended, because it separates the scopes of variables used in views and partials:

#view index.html.erb 
<%= @var %><br/>
<%= render 'foo', var: @var %><br/>
<%= @var %>

#partial _foo.html.erb
<%= var = 2 %>

result:

1
2
1
share|improve this answer
    
This is a good point, I did not think about the mutability of the variables. I had the assumption of the controller "presenting" the variables to the views but instance variables can definitely introduce bugs if they are modified. – Amala Nov 13 '15 at 16:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.