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:


4:

<%= pluralize(@sub_category.errors.count, “error”) %>
prohibited this sub_category from being saved:

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

On Dec 7, 11:10am, Mathew B. [email protected] wrote:

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 C. wrote in post #1035546:

On Dec 7, 11:10am, Mathew B. [email protected] wrote:

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

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.

Michael P. wrote in post #1035550:

On 7 December 2011 11:39, Mathew B. [email protected] wrote:

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?

On 7 December 2011 11:39, Mathew B. [email protected] wrote:

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.

On 7 December 2011 11:54, Mathew B. [email protected] wrote:

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

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.

On 7 Dec 2011, at 12:14, Mathew B. [email protected] wrote:

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

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:


4:

<%= pluralize(@item.errors.count, “error”) %> prohibited
this item from being saved:

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.

On 7 December 2011 13:12, Mathew B. [email protected] wrote:

Fred

Shouldn’t be… there are 4 items in the Item table.

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

On 7 December 2011 13:12, Mathew B. [email protected] wrote:

Frederick C. wrote in post #1035564:

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

Shouldn’t be… there are 4 items in the Item table.

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.

Frederick C. wrote in post #1035564:

On 7 Dec 2011, at 12:14, Mathew B. [email protected] wrote:

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

Shouldn’t be… there are 4 items in the Item table.

On 7 Dec 2011, at 14:01, Mathew B. [email protected] wrote:

(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

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])

if I enter the line above to the rdb command it sets the variable @item
to the correct result.

Oh yeah and using the Item.find(params[:id]) will get this result:
ActiveRecord::StatementInvalid Exception: SQLite3::SQLException: no such
column: items.: SELECT “items”.* FROM “items” WHERE “items”."" = ?
LIMIT 1

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs