Last Update : Jan 2010

Variables you shouldn't use in Rails controllers

I ran into this issue and spent an hour debugging this.  Writing it here, so it might help some one else.

Lets create a simple RAILS project

rails hello
cd hello
./script/generate controller hello

Add a simple index method

app/controller/hello_controller.rb
class HelloController < ApplicationController
  def index
  end
end


app/views/hello/index.html.erb
<html>
  <title>
  </title>
  <body>
    <h1>Hello world</h1>
    <%= link_to 'index', :controller => 'hello', :action => 'index' %>
  </body>
</html>

In your browser go to url http://localhost:3000/hello  .  You wil get the simple greeting



All is fine.


Now lets add an instance variable in the controller

app/controller/hello_controller.rb
class HelloController < ApplicationController
  def index
    @url = "http://www.sujee.net"
  end
end

Do a reload....

Awww....

undefined method `rewrite' for "http://sujee.net":String



So our innocent little variable caused this!!

What is going on here?  Turned out we inadvertantly over-wrote an internal variable Rails uses; that happens to be @url.

If we change this variable to say '@myurl' things start to work again.

How can we find out what variables are being used?  Lets update our controller

app/controller/hello_controller.rb
class HelloController < ApplicationController
  def index
    puts self.instance_variables
  end
end

Reload the page.  And look at the console where 'script/server' is running.

=> Booting WEBrick
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-01-28 23:58:10] INFO  WEBrick 1.3.1
[2010-01-28 23:58:10] INFO  ruby 1.8.7 (2008-08-11) [universal-darwin10.0]
[2010-01-28 23:58:10] INFO  WEBrick::HTTPServer#start: pid=94840 port=3000
@_session
@_params
@url
@performed_redirect
@request_origin
@_request
@_headers
@performed_render
@action_name
@template
@_response
@before_filter_chain_aborted


Processing HelloController#index (for 127.0.0.1 at 2010-01-28 23:58:14) [GET]
Rendering hello/index
Completed in 79ms (View: 77, DB: 0) | 200 OK [http://localhost/hello]


 We over-wrote @url and caused the problem.


There you go, don't name your variables as one of these:
  • @_session
  • @_params
  • @url
  • @performed_redirect
  • @request_origin
  • @_request
  • @_headers
  • @performed_render
  • @action_name
  • @template
  • @_response
  • @before_filter_chain_aborted

Thanks a bunch to my collegue Go Kojima for helping me debug this issue.


** Comment on this article **