Rails - undefined method `headers'

Discussion in 'Ruby' started by Clay Culver, Oct 25, 2005.

  1. Clay Culver

    Clay Culver Guest

    Sorry for the complete newbie question, but this seems like it should
    be so obvious. I've created a Controller called Welcome. The
    welcome_controller.rb contains this:

    class WelcomeController < ApplicationController
    def index
    @response = "hello world"
    end
    end

    I have a view for welcome called "index.rhtml" that contains this:

    <html>
    <head>
    <title>Test</html>
    </head>
    <body>
    <%= @responce %>
    </body>
    </html>

    This seems like it would be a no brainer, but when I go to the page I
    get this error:

    undefined method `headers' for "":String
    Extracted source (around line #46):

    43:
    44:
    45: <h2 style="margin-top: 30px">Response</h2>
    46: <b>Headers</b>: <%=h @response.headers.inspect.gsub(/,/, ",\n")
    %><br/>

    ---

    What exactly am I doing wrong? I know I could just use render_text but
    I'm trying to do something more complex, but I can't seem to make this
    work. =/
     
    Clay Culver, Oct 25, 2005
    #1
    1. Advertising

  2. 1. This is the Ruby general list. Please check out the Ruby on Rails
    mailing list at http://lists.rubyonrails.org/mailman/listinfo
    2. Choose a different variable name than @response. You're getting a
    name clash with a variable Rails uses internally.

    Devin

    Clay Culver wrote:

    >Sorry for the complete newbie question, but this seems like it should
    >be so obvious. I've created a Controller called Welcome. The
    >welcome_controller.rb contains this:
    >
    >class WelcomeController < ApplicationController
    > def index
    > @response = "hello world"
    > end
    >end
    >
    >I have a view for welcome called "index.rhtml" that contains this:
    >
    ><html>
    ><head>
    ><title>Test</html>
    ></head>
    ><body>
    > <%= @responce %>
    ></body>
    ></html>
    >
    >This seems like it would be a no brainer, but when I go to the page I
    >get this error:
    >
    >undefined method `headers' for "":String
    >Extracted source (around line #46):
    >
    >43:
    >44:
    >45: <h2 style="margin-top: 30px">Response</h2>
    >46: <b>Headers</b>: <%=h @response.headers.inspect.gsub(/,/, ",\n")
    >%><br/>
    >
    >---
    >
    >What exactly am I doing wrong? I know I could just use render_text but
    >I'm trying to do something more complex, but I can't seem to make this
    >work. =/
    >
    >
    >
    >
    >
     
    Devin Mullins, Oct 25, 2005
    #2
    1. Advertising

  3. Clay Culver

    Clay Culver Guest

    Whoops, looks like @responce is for other things. I didn't realize
    that. =/
     
    Clay Culver, Oct 25, 2005
    #3
  4. Clay Culver

    Dave Burt Guest

    Clay Culver asked:
    > Sorry for the complete newbie question, but this seems like it should
    > be so obvious. I've created a Controller called Welcome. The
    > welcome_controller.rb contains this:
    >
    > class WelcomeController < ApplicationController
    > def index
    > @response = "hello world"
    > end
    > end
    >
    > I have a view for welcome called "index.rhtml" that contains this:
    >
    > <html>
    > <head>
    > <title>Test</html>
    > </head>
    > <body>
    > <%= @responce %>
    > </body>
    > </html>
    >
    > This seems like it would be a no brainer, but when I go to the page I
    > get this error:
    >
    > undefined method `headers' for "":String
    > Extracted source (around line #46):
    >
    > 43:
    > 44:
    > 45: <h2 style="margin-top: 30px">Response</h2>
    > 46: <b>Headers</b>: <%=h @response.headers.inspect.gsub(/,/, ",\n")
    > %><br/>
    >
    > ---
    >
    > What exactly am I doing wrong? I know I could just use render_text but
    > I'm trying to do something more complex, but I can't seem to make this
    > work. =/


    You have two problems:
    1) You overwrote the built-in variable @response with a string ("hello
    world")
    2) You misspelt "@responce" in your view.

    The answer is simply to change the name of your variable to something Rails
    isn't trying to use behind the scenes.

    Finally, prefer the Rails community for questions like these: the Freenode
    IRC channel and the mailing list.

    Cheers,
    Dave
     
    Dave Burt, Oct 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dont bother
    Replies:
    0
    Views:
    868
    dont bother
    Mar 3, 2004
  2. Phil
    Replies:
    4
    Views:
    734
    Gabriel Genellina
    Jan 17, 2010
  3. smc
    Replies:
    1
    Views:
    342
  4. Ian
    Replies:
    2
    Views:
    2,113
  5. Daniel Mendler

    Undefined method errors in rails

    Daniel Mendler, Mar 14, 2008, in forum: Ruby
    Replies:
    0
    Views:
    145
    Daniel Mendler
    Mar 14, 2008
Loading...

Share This Page