Could someone please explain how to create a dynamically generated pull-down select that shows name labels to the web visitor and writes the corresponding ID to the database? This concept seems so basic that it must be obvious to everyone else but I am completely at wit's end trying to find a way to make this work and/or a good code example for learning how to do this. Any suggestions for a very frustrated newbie?
I have a simple category model using Awesome Nested List to track about 200 categories. These are the table fields:
t.string :name
t.integer :parent_id
t.integer :lft
t.integer :rgt
This is the category.rb model:
class Category < ActiveRecord::Base
acts_as_nested_set
attr_accessible :name, :parent_id
end
This is the Simple Form for view/categories/_form.html.erb
<%= simple_form_for(@category) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name, :label => 'Category name' %>
<%= f.select :parent_id, :label => 'Parent category', :value_method => { |r| [r.name, r.id, { :class => r.category.id }]}, :include_blank => true %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
This yields a "SyntaxError in Categories#new" error message
Extracted source (around line #6):
3:
4: <div class="form-inputs">
5: <%= f.input :name, :label => 'Category name' %>
6: <%= f.select :parent_id, :label => 'Parent category', :value_method => { |r| [r.name, r.id, { :class => r.category.id }]}, :include_blank => true %>
7: </div>
8:
9: <div class="form-actions">
You can use awesome_nested_set's view helper for building your select
:
<%= f.select :parent_id, nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" } %>