---
title: "Discussion on \"Refactoring instance variables to local variables in Rails controllers\" | Hashnode"
description: "Discussion on \"Refactoring instance variables to local variables in Rails controllers\". An example of refactoring a controller to use local variable and shorthand hash syntax"
url: "https://hashnode.com/posts/refactoring-instance-variables-to-local-variables-in-rails-controllers/63c77761dc79800548ce91b1"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Refactoring instance variables to local variables in Rails controllers](https://codeandgit.hashnode.dev/refactoring-instance-variables-to-local-variables-in-rails-controllers?utm_source=hashnode&utm_medium=feed)

An example of refactoring a controller to use local variable and shorthand hash syntax

Jan 18, 2023 · 9 min read · codeandgit.hashnode.dev  
Updated Jan 20, 2023  
Read the article

-   [#ruby](https://hashnode.com/tag/ruby)
-   [#refactoring](https://hashnode.com/tag/refactoring)
-   [#programming-blogs](https://hashnode.com/tag/programming-blogs)
-   [#programming-tips](https://hashnode.com/tag/programming-tips)
-   [#programming-languages](https://hashnode.com/tag/programming-languages)

## Written by

[Lucian Ghinda](https://hashnode.com/@lucianghinda)

@lucianghinda · Europe

  
[View profile](https://hashnode.com/@lucianghinda)

Senior Product Engineer

Senior Product Engineer, working in Ruby and Rails. Passionate about idea generation, creativity, and programming. I curate the Short Ruby Newsletter.

## Responses 1

Markdown supported · ⌘↵ to post

[Paweł Świątkowski](https://hashnode.com/@katafrakt)

[Jan 18, 2023](https://hashnode.com/posts/refactoring-instance-variables-to-local-variables-in-rails-controllers/63c77761dc79800548ce91b1/comment/63c7baeaedc601710bd097f5)

I really like the idea in the post! For some time already I feel that passing data between the controller and the view by setting an instance variable is one of the biggest and most fundamental design flaws of Rails – and I think you provide great examples of how to avoid it.

However, personally I'm not so sure about this keeping the controller action as a one-line render-only thing. Controllers are procedural in nature and I actually like to see what is happening in a step-by-step way. I can understand the allure of hiding the complexity of filtering books in a private method, but honestly I would prefer to at least set the user directly:

```
def index
  user = User.find(params[:user_id])
  books = get_filtered_books
  render locals: { user:, books: }
end
```

I wonder what is your take on that.
