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?
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):
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.
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:
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.
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.
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.