Nil is not a symbol error.

Hi all,

As part of a school project I need to set up an items collection which
has categories and subcategories. I have used scaffolding to create
these but when i edited the subcategory controller file by inserting
this into the edit function: @category = Category.find_by_id(:all) and
edited the model sub_category.rb to have the following line in it:

belongs_to :category

Now my problem is that when I now click the edit button it comes up with
an error:

TypeError in Sub_categories#edit
Showing ItemCollection/app/views/sub_categories/_form.html.erb where
line #1 raised:

nil is not a symbol
Extracted source (around line #1):

1: <%= form_for(@sub_category) do |f| %>
2: <% if @sub_category.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@sub_category.errors.count, "error") %>
prohibited this sub_category from being saved:</h2>

I even removed my changes to those files and the error still appears.
Anyone have any ideas what might be causing this?

Hi all,

As part of a school project I need to set up an items collection which
has categories and subcategories. I have used scaffolding to create
these but when i edited the subcategory controller file by inserting
this into the edit function: @category = Category.find_by_id(:all) and
edited the model sub_category.rb to have the following line in it:

belongs_to :category

Now my problem is that when I now click the edit button it comes up with
an error:

TypeError in Sub_categories#edit
Showing ItemCollection/app/views/sub_categories/_form.html.erb where
line #1 raised:

nil is not a symbol
Extracted source (around line #1):

1: <%= form_for(@sub_category) do |f| %>

That suggests that @sub_category is nil. Is it?

Fred

Frederick Cheung wrote in post #1035546:

It's not being set to "SubCategory.find_by_id", it's being set to the
result of the expression "SubCategory.find_by_id(:all)", which doesn't
seem to me to be likely to return any records, as ":all" is a Symbol,
and find_by_id would probably expect an integer ID or array of
integers.

Michael Pavling wrote in post #1035550:

That was probably the problem since @sub_category was being set to
SubCategory.find_by_id... so I will try and create it again and now just
have it as one word to avoid confusion.

It's not being set to "SubCategory.find_by_id", it's being set to the
result of the expression "SubCategory.find_by_id(:all)", which doesn't
seem to me to be likely to return any records, as ":all" is a Symbol,
and find_by_id would probably expect an integer ID or array of
integers.

I re-scaffolded everything and now the class that used to be called
sub_category is now just subcategory. The same error appears as in my
first post. And the following is the inside of the edit function in the
subcategories_controller:

@subcategory = Subcategory.find_by_id(params[:id])
@category = Category.find(:all)

the model file (subcategory.rb):
class Subcategory < ActiveRecord::Base
  belongs_to :category
end

Any advice?

Firstly, watch out for naming convention issues. "Category.find(:all)"
is going to return an array of Category objects, so the instance
variable they're assigned to should really be called "@categories" not
"@category".

If you're still having problems, and you've been re-creating stuff,
we're not going to glean anything from looking at your first post.
Copy the error here again. It also tells you which file and line
number the error is in, so please post the code from around that area
too, so we can see exactly what your current situation is.

Thanks for your reply!

Well since the same problem arises with the Item object.. I will post
the errors from there:

TypeError in Items#edit

Showing ItemCollection/app/views/items/_form.html.erb where line #1
raised:

nil is not a symbol
Extracted source (around line #1):

1: <%= form_for(@item) do |f| %>
2: <% if @item.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@item.errors.count, "error") %> prohibited
this item from being saved:</h2>

From the controller file:
def edit
  @item = Item.find_by_id(params[:id])
end

The interesting thing is that show method works with the following code:
  def show
    @item = Item.find_by_id(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @item }
    end
  end

I tried adding the respond_to part to edit method and changed the show
to edit but it still produces the same error.

Thanks for your reply!

Well since the same problem arises with the Item object.. I will post
the errors from there:

TypeError in Items#edit

Showing ItemCollection/app/views/items/_form.html.erb where line #1
raised:

nil is not a symbol
Extracted source (around line #1):

1: <%= form_for(@item) do |f| %>

So, at the risk of repeating myself, is @item nil?

Fred

Frederick Cheung wrote in post #1035564:

The question is not whether it should be but whether it is.
Have a look at the Rails Guide on debugging to find out how to use
ruby-debug to break into your code and inspect data and follow the
flow. Then you can break in after @item is created you can have a
look. You will also find other suggestions for debugging there.

Colin

riiiiight... but does one of have the id that you're selecting through
params? :wink:
If not, @item will be nil.

Try replacing :
  @item = Item.find_by_id(params[:id])
wiht
  @item = Item.find(params[:id])

...as find will raise an exception if it doesn't find a matching record.

Then have a look at how to do use the debugger in Rails to inspect
your code as it runs.

Then have a look at how to do use the debugger in Rails to inspect
your code as it runs.

Thanks a lot for everyone who has replied!

So I have started running the debugger and I get some weird results:

@item = Item.find_by_id(params[:id])
(rdb:2) @item
nil
(rdb:2) Item.find_by_id(params[:id])
#<Item id: 3, title: "Hello World!", location: "On the desk", comments:
"***", created_at: "2011-12-07 11:43:03", updated_at: "2011-12-07 11:43
:03">

Anyone know why it isn't setting the result of
Item.find_by_id(params[:id]) to the variable @item although i
specifically tell it to do so:

@item = Item.find_by_id(params[:id])

Then have a look at how to do use the debugger in Rails to inspect
your code as it runs.

Thanks a lot for everyone who has replied!

So I have started running the debugger and I get some weird results:

@item = Item.find_by_id(params[:id])
(rdb:2) @item
nil
(rdb:2) Item.find_by_id(params[:id])
#<Item id: 3, title: "Hello World!", location: "On the desk", comments:
"***", created_at: "2011-12-07 11:43:03", updated_at: "2011-12-07 11:43
:03">

Anyone know why it isn't setting the result of
Item.find_by_id(params[:id]) to the variable @item although i
specifically tell it to do so:

@item = Item.find_by_id(params[:id])

The debugger isn't quite an irb prompt (although you can get one by typing irb if my memory is correct)

Fred