SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem trying to display a variable

    I feel stupid even asking this as it seems like it should be so simple. I am just working through the basics and trying to customize my scaffold. I am working on the show action right now.

    Here is what I have for the show action in the controller
    Code:
      def show
        @blog = Blog.find(params[:id])
      end
    And here is what I have in my view
    Code:
    <h2><%= link_to @blog["title"], :action => 'show', :id => blog %></h2>
    <p><%= @blog["body"] %></p>
    <p><small><%= @blog["created_at"] %></small></p>
    This is the error I get: undefined local variable or method `blog' for #<#<Class:0x35ed30c>:0x35ed2e4>

    Can anyone help me out with this?

  2. #2
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jhorra View Post
    I feel stupid even asking this as it seems like it should be so simple. I am just working through the basics and trying to customize my scaffold. I am working on the show action right now.

    Here is what I have for the show action in the controller
    Code:
      def show
        @blog = Blog.find(params[:id])
      end
    And here is what I have in my view
    Code:
    <h2><%= link_to @blog["title"], :action => 'show', :id => blog %></h2>
    <p><%= @blog["body"] %></p>
    <p><small><%= @blog["created_at"] %></small></p>
    This is the error I get: undefined local variable or method `blog' for #<#<Class:0x35ed30c>:0x35ed2e4>

    Can anyone help me out with this?
    what is that @blog["body"]?

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @blog["body"] is an alternative syntax for @blog.body. I think it's better to use @blog.body because (1) it's shorter (2) you'll get error messages if you do @blog.bosy but not if you do @blog["bosy"]. You can use @blog[the_attribute] if you have the attribute name in a variable.

    problem is here (^^^^):

    Code:
    <h2><%= link_to @blog["title"], :action => 'show', :id => blog %></h2>
                                                              ^^^^
    You have to use @blog instead of blog.

  4. #4
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh, I never even looked at that one. I had @blog.title at first, and changed it a bunch of different ways because I thought that was the problem.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •