SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lenz book - Chapter 7 - Page 199... HELP.

    I am getting an error... I am using Ruby 1.8.4, Rails 2.1.0.

    -----
    Code:
    NoMethodError in Story#show
    
    Showing story/show.rhtml where line #1 raised:
    
    You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.name
    
    Extracted source (around line #1):
    
    1: <h2><&#37;= @story.name %></h2>
    2: <p><%= link_to @story.link, @story.link %></p>
    -----

    I have ensured that the permalinks exist... It's driving me NUTS.

    Code below...

    Story_controller.rb

    Code:
    Code Ruby:
    class StoryController < ApplicationController
      skip_before_filter :verify_authenticity_token
     
      def show
        @story = Story.find_by_permalink(params[:permalink])
      end 
      def index
        @story = Story.find(:first, :order => 'RAND()')
      end
      def new
        @story = Story.new(params[:story])
        if request.post? and @story.save
          flash[:notice] = 'Story submitted!'
          redirect_to :action => 'index'
        end
      end
    end
    I have checked this over and over... If someone could help, please do.

    EDIT:

    Okay, I even resorted to copying and pasting from the code archive. I AM FULL OF FAIL. Bleh. It didn't work. Perhaps the problem is with Rails 2.1.0. ???

    EDIT 2:

    What... The... F --

    It works now... ? I just had to restart WEBrick.

    ...

    Whatever, eh? Mods, delete this thread whenever... Sorry.
    Last edited by grenadier42; Jul 3, 2008 at 23:23.

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you say Lenz book, do you mean the first one, Build Your Own Ruby on Rails Web Applications, and not Simply Rails 2?

    I don't have a copy of the first book so I can't say exactly what the problem is, Rails 2.1 is quite different from Rails 1.2 (which I assume is what the first book uses), so that could be the problem but it also might not.

    The error you are getting is because find is not finding a record. Do you have a permalink column in your table? Do all the stories in your database have permalinks attached? I notice that your index pulls in the only one random record, it's possible it pulls in one that doesn't have a permalink.

  3. #3
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    I have got the exact same problem...
    I have checked everything I think including spelling, the tests run fine.
    I am using Ruby 2.1 and I have got the "Build Your Own Ruby on Rails Web Applications book.

    this is the code from the controller:

    class StoryController < ApplicationController
    def index
    @story = Story.findfirst, rder => 'RAND()')
    end

    def new
    @story = Story.new(params[:story])
    if request.post? and @story.save
    flash[:notice] = 'Story submission succeeded'
    redirect_to :action => 'index'
    end
    end

    def show
    @story = Story.find_by_permalink(params[ermalink])
    end

    end

    and this is the code from the view

    <h2><%= @story.name %></h2>
    <p><%= link_to @story.link, @story.link %></p>


    Hopefully, someone can help me...
    thanks a lot!
    best,
    Diederik

  4. #4
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have got the exact same problem...
    I have checked everything I think including spelling, the tests run fine.
    I am using Ruby 2.1 and I have got the "Build Your Own Ruby on Rails Web Applications book.

    this is the code from the controller:

    Code Ruby:
    class StoryController < ApplicationController
      def index
        @story = Story.find(:first, :order => 'RAND()')
      end
     
      def new
        @story = Story.new(params[:story])
    	if request.post? and @story.save
    	  flash[:notice] = 'Story submission succeeded'
    	  redirect_to :action => 'index'
    	end
      end
     
      def show
        @story = Story.find_by_permalink(params[:permalink])
      end 
     
    end

    and this is the code from the view

    Code Ruby:
    <h2><%= @story.name %></h2>
    <p><%= link_to @story.link, @story.link %></p>


    Hopefully, someone can help me...
    thanks a lot!
    best,
    Diederik

  5. #5
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like Delameko says: do all of your records definitely have something in their permalink fields?

  6. #6
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I go to IRB mode then the story.find_by_permalink function does work.....so I am positive that all fields have a permalink value (I also checked this in the mysql tables)

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about the name field? It's trying to display @story.name on the view page, if the name isn't there then it will also throw this error.

  8. #8
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The exact error message is:

    You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.name

    If I go to the Ruby console and enter:
    s = Story.find_by_name('foo')
    then it will find story 'foo'

    It is really driving me nuts but thanks for your questions, hopefully you have some more suggestions on how to fix this!
    best,
    Diederik

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by diederikvanliere View Post
    If I go to the Ruby console and enter:
    s = Story.find_by_name('foo')
    then it will find story 'foo'
    Having typed that into the console, now type:

    Code:
    s.name
    What do you get?

  10. #10
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I enter s.name then I get
    => foo

    so that seems to work......

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I misread. Try this instead:

    Code:
    s = Story.find_by_name('foo')
    s.permalink

  12. #12
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if I enter s.permalink it returns 'foo' but that's because the permalink is also called foo, so the permalink variable does exist and does have an appropriate value......

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does it work if you do:
    Code:
      def show
        @story = Story.find_by_permalink('foo')
      end
    At the end of the day, ActiveRecord isn't finding your foo permalink, so something's wrong somewhere. It's hard to debug without seeing everything.

    Is permalink even being passed to the show action? When you get this error:
    Code:
    You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.name
    Look further down the page and you'll see a list of parameters that are being passed. Make sure there's one that says ermalink => 'foo'.

    If it's not being passed at all, have you set to_param in your model.
    Code:
    # in story.rb
    def to_param
      permalink
    end

  14. #14
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I found the error:
    it says {"id"=>"foo"} instead of {"permalink" => "foo"} so somewhere the permalink, maybe in the route.rb file? has not been set up correctly. do you know how to fix this? You are really helpful!!! thanks a lot!
    best,
    Diederik

  15. #15
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I've just realised the error, change your controller to:

    Code:
    @story = Story.find_by_permalink(params[:id])

  16. #16
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code is not Rails 2.1. If you try to use old ruby rails code with 2.1, it simply won't work.

    Here are the solutions to make your code work with rails 2.1.

    Code:
    stories_controller.rb
    class StoriesController < ApplicationController
      def index
    	@story = Story.find(:first, :order => 'RANDOM()')
      end
      def new
    	@story = Story.new
      end
      def create
     	@story = Story.new(params[:story])
    	if @story.save
    	flash[:notice] = 'Story submission succeeded'
    	redirect_to stories_path
    	else
    	render :action => 'new'
    	end
      end
      def show
    	@story = Story.find(params[:id])
      end
    end
    
    <h2>
      <%= @story.name %>
    </h2>
    <p>
      <%= link_to @story.link, @story.link %>
    </p>
    This is the show.html.erb

    I highly reccommend you get the Simply Rails 2 book. It covers the new ruby rails version.

  17. #17
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Delameko,

    Thank you soo much!!!!! That is indeed the answer, it works now fine. Apparently I had become blinded by my own code and my newbie status to Ruby. Again, thanks so much, you have been really helpful!
    best,
    Diederik


Tags for this Thread

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
  •