user2997817
user2997817

Reputation: 31

Rails force instance variable declaration before use on view

Is it possible to force Ruby/Rails to throw an error when printing/using instance variables on a view that haven't been defined on controller

I'm declaring an instance variable on a Rails Controller and I'm printing its value on a View

def controller_action
    @some_data = "some value"
end

Then we know we can print its value on a view

<p>Some data has <%= @some_data %></p>

My problem is when doing mistakes on a view like this:

<p>Some data has <%= @somedata %></p>

Ruby won't complain and it's difficult to find those mistakes. This also applies for team development where some programmer can create an instance variable on a controller with one name and another programmer expects to print it on a view but accidentally uses other name.

Upvotes: 2

Views: 614

Answers (3)

Ismael
Ismael

Reputation: 16710

Simple solution: Use locals instead of instance variables

def controller_action
  render locals: { some_data: "some value" }
end

And then you can use it as a local variable/method

<p>Some data has <%= some_data %></p>

If you don't pass it, or if you do not access it properly (with a typo) you will get a no variable/method defined error.

Upvotes: 1

Rob Wise
Rob Wise

Reputation: 5120

Ugly Way

As @BartJedrocha said, you could manually code it at the top of your view:

<% raise "Instance variable @somedata not defined" unless defined?(@somedata) %>

You could even take it a step further and make a method in a helper that could take an array as an argument and check that each instance variable in the array is defined and raise an error if not. That's pretty annoying, though, and I haven't seen anyone doing that.


Good Way via Better_Errors Gem

I think a better solution is to only pass a single instance variable from your controller to your view, which is considered a best practice. This will cut down on the opportunities for this type of error to occur.

Additionally, while I try not to recommend code libraries as solutions, this one is so universal in Rails that I think it is acceptable: the Better_Errors gem will result in an error page that clearly lists all instance variables available to your view and would make it trivial to determine whether there is a problem there (notice the lower right section in the picture below).

Better Errors Page

The live shell will allow you to use REPL to interact with the current state to further investigate issues.


Weird Way via "Verbose" Mode

Lastly, I supposed you could attempt to run Rails in verbose mode. Verbose mode can be set when using Ruby from the command line with the -w flag and will give a warning if you attempted to use an undefined instance variable check out this resource for more. However, we are using Rails and not manually invoking Ruby ourselves, so in order to pass that in, you would probably need to set the environment variable ($VERBOSE=true) in a config file.

Upvotes: 3

user2064084
user2064084

Reputation: 5

Rails by default will not provide any exceptions if the variable is not declared in the controller and instead it will just treat is as a 'nil'. But even if you declare the object in controller there is still possibility for the variable to become nil. So it will be difficult to find whether the variable is nil because it was not declared or it got a nil value.

We may be able to replace the nil in rails using the to_s conversion. So please try some methods like that instead of raising an error or exception.

Upvotes: 0

Related Questions