SitePoint Sponsor |
|
User Tag List
Results 1 to 17 of 17
-
Jul 3, 2008, 23:36 #1
- 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><%= @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
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 4, 2008 at 00:23.
-
Jul 4, 2008, 00:48 #2
- 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.
-
Jul 20, 2008, 17:54 #3
- 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:
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
-
Jul 20, 2008, 17:55 #4
- 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
-
Jul 21, 2008, 03:55 #5
- 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?
-
Jul 21, 2008, 12:02 #6
- 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)
-
Jul 22, 2008, 00:16 #7
- 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.
-
Jul 22, 2008, 07:04 #8
- 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 nutsbut thanks for your questions, hopefully you have some more suggestions on how to fix this!
best,
Diederik
-
Jul 22, 2008, 07:15 #9
- Join Date
- May 2008
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jul 22, 2008, 07:45 #10
- 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......
-
Jul 22, 2008, 07:59 #11
- 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
-
Jul 22, 2008, 08:18 #12
- 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......
-
Jul 23, 2008, 00:23 #13
- 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
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
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
-
Jul 23, 2008, 07:28 #14
- 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
-
Jul 23, 2008, 08:25 #15
- 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])
-
Jul 23, 2008, 08:29 #16
- 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>
I highly reccommend you get the Simply Rails 2 book. It covers the new ruby rails version.
-
Jul 23, 2008, 08:30 #17
- 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
Bookmarks