public
Created

dynamic menu

  • Download Gist
Category.rb
Ruby
1 2 3 4 5 6 7 8 9
class Category < ActiveRecord::Base
acts_as_nested_set
accepts_nested_attributes_for :children
attr_accessible :name, :parent_id
attr_protected :lft, :rgt
has_many :products
end
_category.html.erb
HTML+ERB
1 2 3 4 5 6
<div id="category_select">
<p id="subcategory_field">
<label for="subcategory">Subcategory</label><br/>
<%= f.collection_select :category_id, @subcategory ,:id,:name,:prompt => "Select Subcategory"%>
</p>
</div>
_form.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12
<p>
<label for="category">Category</label><br/>
<%= f.collection_select :category, Category.find_all_by_parent_id(nil) ,:id,:name,:prompt => "Select category"%>
</p>
 
<%= render :partial => 'category', :locals => { :f => f , :categoryID => @subcategory} %>
 
#if without :partial ,it will give me error ,f is undefined method
#if with :partial, it give me nil object when you didn't expect it!
application.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
$(document).ready(function () {
 
$('#subcategory_field').hide();
 
$('#product_category').change(function () {
$('#subcategory_field').show();
 
data = $('#product_category').val();
jQuery.ajax({
url: '/javascripts/dynamic_subcategory/',
type: 'GET',
data: {"categoryID" : data}
});
return false;
});
});
 
 
 
#no changes
console server errors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Started GET "/products/new" for 127.0.0.1 at 2012-06-30 04:12:43 -0400
Processing by ProductsController#new as HTML
SQL (0.3ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
Category Load (0.4ms) SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" IS NULL
Rendered products/_category.html.erb (5.1ms)
Rendered products/_form.html.erb (159.5ms)
Rendered products/new.html.erb within layouts/application (160.5ms)
Completed 500 Internal Server Error in 221ms
 
ActionView::Template::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.map):
1: <div id="category_select">
2: <p id="subcategory_field">
3: <label for="subcategory">Subcategory</label><br/>
4: <%= f.collection_select :category_id, @subcategory ,:id,:name,:prompt => "Select Subcategory"%>
5: </p>
6: </div>
app/views/products/_category.html.erb:4:in `_app_views_products__category_html_erb___771868271907344220_33660280_35256791884167728'
app/views/products/_form.html.erb:20:in `block in _app_views_products__form_html_erb__4356573898125156143_34174760__1131280712954032946'
app/views/products/_form.html.erb:5:in `_app_views_products__form_html_erb__4356573898125156143_34174760__1131280712954032946'
app/views/products/new.html.erb:3:in `_app_views_products_new_html_erb___4226790253553222994_34189920_4007620194630562797'
app/controllers/products_controller.rb:31:in `new'
 
Rendered /home/nicholas/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /home/nicholas/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.8ms)
Rendered /home/nicholas/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (6.9ms)
dynamic_subcategory.js.erb
HTML+ERB
1 2 3
$('category_select').replaceWith('<%= escape_javascript(render => 'products/category', :locals => {:f => f,:categoryID => @subcategory}) %>')
 
#added :f => f
javascripts_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class JavascriptsController < ApplicationController
respond_to :js
 
def dynamic_subcategory
@subcategory = Category.find(params[:categoryID]).children
 
respond_with(@subcategory) # This will return dynamic_subcategory.js.erb
end
 
end
i change responde_with :js to respond_to :js
 
 
#when i load the page,it return me 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.map and error come from
<%= f.collection_select :category_id, @subcategory,:id,:name,:prompt => "Select Subcategory"%>
 
,i see the terminal, and it dint load my js files.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.