I think I am going blind, because I cannot find what is wrong with the
following. The bad thing is that I had it working, made couple of
changes, reverted back to the original, now it doesn’t work.
I have been trying to figure out why this isn’t working way too long,
to the point my vision is blurring.
<% form_for :categories, :url => {:action => "show_tasks"} do |form|
%>
Select Category:
<%= form.collection_select :name, @categories, :id, :name %>
<%= submit_tag "View" %>
<% end %>
here is the error message
NoMethodError in Mytasks#index
Showing app/views/mytasks/index.rhtml where line #10 raised:
undefined method `name’ for #Array:0xb7611edc
Extracted source (around line #10):
7:
8: <% form_for :categories, :url => {:action => “show_tasks”} do |
form| %>
9: Select Category:
10: <%= form.collection_select :name, @categories, :id, :name %>
11: <%= submit_tag “View” %>
12: <% end %>
13:
Thanks for the help
On 8-Apr-07, at 4:58 PM, chris wrote:
%>
undefined method `name’ for #Array:0xb7611edc
13:
Chris, @categories is your problem.
collection_select is iterating through @categories looking for
category.name.
Take a look at your controller where you’re populating @categories.
Cheers,
Jodi
General Partner
The nNovation Group inc.
www.nnovation.ca/blog
Sorry, I should’ve mentioned that there has been no changes to the
controller since I had it working.
Here is the index method from the controller
def index
@categories = Category.find(:all)
@tasks = []
end
But the error is in regards to to the *select control. If I remove
line 10 from the index.rhtml file it renders to the browser without
any errors.
Hi Chris, you should be able to do the following:
<%= form.collection_select ‘task’, ‘category_id’, @categories, ‘id’,
‘<some_category_attribute>’ %>
This invocation will generate a select tag name=“task[category_id]” with
options of form category.<some_category_attribute>. The
option with category.id == user.category_id will be selected.
Now, I’m guessing the following from you previous e-mail:
task belongs_to category
category has_many tasks
Thus, I’m seeing the following relationship:
class Task < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :tasks
end
Please note that Task model contains the foreign key, ‘category_id’,
because it has the belongs_to declaration.
Good luck,
-Conrad
maybe this will help. I totally removed the linkage to the categories
and tried to hard code some values, with some random names
<% form_for :categories, :url => {:action => "show_tasks"} do |form|
%>
Select Category:
<%= form.select "some_names", %w{Chris, Andy, Wally} %>
<%= submit_tag "View" %>
<% end %>
Here is the error message
NoMethodError in Mytasks#index
Showing app/views/mytasks/index.rhtml where line #10 raised:
undefined method `some_names’ for #Array:0xb757f654
Extracted source (around line #10):
7:
8: <% form_for :categories, :url => {:action => “show_tasks”} do |
form| %>
9: Select Category:
10: <%= form.select “some_names”, %w{Chris, Andy, Wally} %>
11: <%= submit_tag “View” %>
12: <% end %>
13:
Since I am just playing around with things I am constantly re-
formatting how things are arranged on the screen. How this is
supposed to work, is that at the top of the screen there is a select
list containing the categories, that combined within the button beside
it, allows the user to easily view the todo’s for each of the
categories.
So the select control that I am having problems with isn’t yet, at the
time of the problem, related to the tasks just yet. I want to fill
the select control with all the categories that exist in the database.
Thanks for the help, I really appreciate it.
Hi, you should be able to do the following:
- Define this in the relevant model.
category_type = [
[ “Chris”, “1” ]
[ “Andy”, “2” ]
[ “Wally”, “3” ]
]
- Add this code to the relevant view.
<%= form.select :some_names, <class_name>::category_type %>
Note: <class_name> := The name of your model class. For example,
Category
Good luck,
-Conrad
Hi, here’s the corrected version:
category_type = [
[ “Chris”, “1” ],
[ “Andy”, “2” ],
[ “Wally”, “3” ]
]
Here is the updated snippet from the rhtml file
++++++++++++++++++
<% form_for :categories, :url => {:action => "show_tasks"} do |form|
%>
Select Category:
<%= form.select :brothers, Category::BROTHERS %>
<%= submit_tag "View" %>
<% end %>
Here is the addition of the BROTHERS array in the category model
++++++++++++++++++
class Category < ActiveRecord::Base
has_many :tasks
BROTHERS = [
[“1”, “Chris”],
[“2”, “Tim”],
[“3”, “Tom”]
]
end
And here is the error
++++++++++++++++++++
NoMethodError in Mytasks#index
Showing app/views/mytasks/index.rhtml where line #10 raised:
undefined method `brothers’ for #Array:0xb7739c4c
Extracted source (around line #10):
7:
8: <% form_for :categories, :url => {:action => “show_tasks”} do |
form| %>
9: Select Category:
10: <%= form.select :brothers, Category::BROTHERS %>
11: <%= submit_tag “View” %>
12: <% end %>
13: