Topic: NoMethodError in Admin/tag#new

I have the following code in the tag.rb model:

TAG_GROUP = [
        #Displayed                stored in db
        ["Name",                "name"],
        ["Relationship",    "relationship"],
        ["Birth",                "birth"],
        ["Death",                "death"],
        ["Burial",                "burial"],
        ["Marriage",            "marriage"],
        ["Divorce",            "divorce"],
        ["History",            "history"],
        ["Address",            "address"],
        ["Other Event",        "other_event"]
    ]

A list of name/value pairs for a select box.  I have the following code in the partial _form.rhtml file:
<p><label for="tag_tag_group">Tag Group</label><br/>
<%= form.select :tag_group, Tag::TAG_GROUP, :prompt=>"Select a tag group"  %></p>

.  This code throws the following error: You have a nil object when you didn't expect it!
You might have expected an instance of Array. The error occurred while evaluating nil.select.  Can anyone tell me why the error?  The code worked before I changed to a select box.

Re: NoMethodError in Admin/tag#new

Open up script/console and make sure Tag::TAG_GROUP is getting set. Or just output it in the view:

<%= Tag::TAG_GROUP.inspect %>

If that is returning nil, where exactly are you defining the constant in the Tag class?

Railscasts - Free Ruby on Rails Screencasts

Re: NoMethodError in Admin/tag#new

When I put the snippet of code on the file it returns the array as expected.  Here is its placement in the tag.rb file

class Tag < ActiveRecord::Base
    TAG_GROUP = [
        #Displayed                stored in db
        ["Name",                "name"],
        ["Relationship",    "relationship"],
        ["Birth",                "birth"],
        ["Death",                "death"],
        ["Burial",                "burial"],
        ["Marriage",            "marriage"],
        ["Divorce",            "divorce"],
        ["History",            "history"],
        ["Address",            "address"],
        ["Other Event",        "other_event"]
    ]
   
    validates_inclusion_of :tag_group, :in=>TAG_GROUP.map {|disp, value| value}
end

.  BTW how do I inspect it usinf script/console?

Re: NoMethodError in Admin/tag#new

Well, if it is outputting the proper result in the view then that probably isn't the problem. If you still want to try script/console, change to your current project directory and run this command:

ruby script/console

You can then run commands like Tag::TAG_GROUP in there and see the output.

As for the problem, I don't see what it could be. The only thing out of the ordinary is the :prompt option, try it without that and see if it is the problem:

<%= form.select :tag_group, Tag::TAG_GROUP  %>

If it still reports the error, try it like this:

<%= form.select :tag_group, ['foo', 'bar']  %>

If that still reports an error, what does the @tag.tag_group return? Is it just a string column in the database or some custom method?

Railscasts - Free Ruby on Rails Screencasts

Re: NoMethodError in Admin/tag#new

All your suggestions returned the same error except @tag.tag_group which returned the array.  The script/console also returned the array.  Does this provide any additional clues?

Re: NoMethodError in Admin/tag#new

Sounds like that's the problem. Rails expects the attribute to return a single string representing the selected value. In other words, @tag.tag_group should return the string value that you want selected in the select box.

The error still doesn't make sense, but that's the only thing that is out of the ordinary.

Railscasts - Free Ruby on Rails Screencasts

Re: NoMethodError in Admin/tag#new

What's the 'form' object that you have in there?  It seems the error might relate to having 'form' set to nil when you tried:
<%= form.select :tag_group, Tag::TAG_GROUP, :prompt=>"Select a tag group"  %></p>

Can you post the entire view code so we can follow along with this?

Re: NoMethodError in Admin/tag#new

Oy, good catch danger. Wasn't even thinking about the form object. smile

Railscasts - Free Ruby on Rails Screencasts

Re: NoMethodError in Admin/tag#new

Here is the _form.rhtml code in full

<%= error_messages_for 'tag' %>

<!--[form:tag]-->
<p><label for="tag_tag_name">Tag name</label><br/>
<%= text_field 'tag', 'tag_name'  %></p>

<p><label for="tag_gedcom_name">Gedcom name</label><br/>
<%= text_field 'tag', 'gedcom_name'  %></p>

<p><label for="tag_lds_only">Lds only</label><br/>
<select id="tag_lds_only" name="tag[lds_only]"><option value="false">False</option><option value="true">True</option></select></p>

<p><label for="tag_tag_group">Tag Group</label><br/>
<%= form.select :tag_group, Tag::TAG_GROUP, :prompt=>"Select a tag group"  %></p>

<p><label for="tag_sentence">Sentence</label><br/>
<%= text_field 'tag', 'sentence'  %></p>
<!--[eoform:tag]-->


and here is the new.rhtml that calls it:
 <h1>New tag</h1>

<% form_tag :action => 'create' do %>
  <%= render :partial => 'form' %>
  <%= submit_tag "Create" %>
<% end %>

<%= link_to 'Back', :action => 'list' %>


Is this what you were looking for?

Re: NoMethodError in Admin/tag#new

Try this instead:

<%= select :tag, :tag_group, Tag::TAG_GROUP, :prompt=>"Select a tag group"  %></p>

Railscasts - Free Ruby on Rails Screencasts

Re: NoMethodError in Admin/tag#new

@ryanb: No problem.  I always let you take the hard ones so it's about time I pulled my weight :-)

@ricklach:
Try changing your form_tag to look like this:

<% form_tag :action => 'create' do |form| %>

That way you'll be able to do things like form.select and form.input

Re: NoMethodError in Admin/tag#new

although, if you're not going to use the form_for syntax (form_for :tag, :url => {:action => 'create'} do |form|) then you may want to remove references to 'form' altogether.  So rather than form.select you'd just have 'select'.