Skip to content

@nichthinkof30 /Category.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
dynamic menu
<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>
<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!
$(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
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
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)
$('category_select').replaceWith('<%= escape_javascript(render => 'products/category', :locals => {:f => f,:categoryID => @subcategory}) %>')
#added :f => f
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.
@FaiqAdam

this is working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.