Reputation: 12873
I am quite the newbie here.
I have been following Railscasts 154 but when I try submitting a comment, I get this error:
undefined method `classify' for nil:NilClass
I little debugging has pointed me to this:
(rdb:5) name =~ /(.+)_id$/
0
debugging name
comes up with micropost_id
but $i is somehow returning nil
.
private
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
debugger
return $i.classify.constantize.find(value)
end
end
nil
end
How do I get past this problem?
Upvotes: 0
Views: 422
Reputation: 38576
This line:
return $i.classify.constantize.find(value)
should be:
return $1.classify.constantize.find(value)
$1
is a global variable storing the first matched group from the regular expression match result.
Upvotes: 1
Reputation: 37357
Looks like $i.classify
should've been $1.classify
. In ruby $1, $2, ...
are global variables which hold the value of the last regexp-matched group. In this case $1 will contain whatever is in parenthesis in your regexp: /(.+)_id$/
.
In your case $i
is nil
, therefore you get error trying to call classify
on nil
.
Upvotes: 1