How to create a dynamic select list using Simple Form and Awesome Nested Set in Rails 3.1? -
could please explain how create dynamically generated pull-down select shows name labels web visitor , writes corresponding id database? concept seems basic must obvious else @ wit's end trying find way make work and/or code example learning how this. suggestions frustrated newbie?
i have simple category model using awesome nested list track 200 categories. these table fields:
t.string :name t.integer :parent_id t.integer :lft t.integer :rgt
this category.rb model:
class category < activerecord::base acts_as_nested_set attr_accessible :name, :parent_id end
this simple form view/categories/_form.html.erb
<%= simple_form_for(@category) |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 "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 building select
:
<%= f.select :parent_id, nested_set_options(category, @category) {|i| "#{'-' * i.level} #{i.name}" } %>
Comments
Post a Comment