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?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Frederick Cheung at Dec 7, 2011 at 11:16 am

    On Dec 7, 11:10 am, Mathew Birch wrote:
    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
    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?

    --
    Posted viahttp://www.ruby-forum.com/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Mathew Birch at Dec 7, 2011 at 11:39 am

    Frederick Cheung wrote in post #1035546:
    On Dec 7, 11:10am, Mathew Birch 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.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Michael Pavling at Dec 7, 2011 at 11:47 am

    On 7 December 2011 11:39, Mathew Birch 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.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Mathew Birch at Dec 7, 2011 at 11:55 am

    Michael Pavling wrote in post #1035550:
    On 7 December 2011 11:39, Mathew Birch 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?

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Michael Pavling at Dec 7, 2011 at 12:06 pm

    On 7 December 2011 11:54, Mathew Birch 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.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Mathew Birch at Dec 7, 2011 at 12:15 pm
    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.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Frederick Cheung at Dec 7, 2011 at 12:58 pm

    On 7 Dec 2011, at 12:14, Mathew Birch wrote:

    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
    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.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Mathew Birch at Dec 7, 2011 at 1:12 pm

    Frederick Cheung wrote in post #1035564:
    On 7 Dec 2011, at 12:14, Mathew Birch 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.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Dec 7, 2011 at 1:15 pm

    On 7 December 2011 13:12, Mathew Birch wrote:
    Frederick Cheung wrote in post #1035564:
    On 7 Dec 2011, at 12:14, Mathew Birch 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.
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Michael Pavling at Dec 7, 2011 at 1:17 pm

    On 7 December 2011 13:12, Mathew Birch wrote:
    Frederick Cheung 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? ;-)
    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.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Mathew Birch at Dec 7, 2011 at 2:01 pm

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

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Frederick Cheung at Dec 7, 2011 at 6:14 pm

    On 7 Dec 2011, at 14:01, Mathew Birch wrote:

    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 7, '11 at 11:11a
activeDec 7, '11 at 6:14p
posts13
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase