I am trying to do the classic category -> subcategory chained dropdowns (where selecting something in category, populates the subcategory).
The code I have works in all browsers except IE (naturally).
Here is the JS code I am using:
$("body select#category").data_binding({
child: "select#company_subcategory_id",
url: "subcategories",
});
[ . . . ]
data_binding: function(options)
{
$(this).change(function()
{
$.getJSON("/"+ options.url +"/",
{ id: $(this).val(), ajax: 'true'},
function(j)
{
for (var i = 0; i < j.length; i++)
{
options += '<option value="' + j[i].optionValue + '">';
options += j[i].optionDisplay;
options += '</option>';
}
开发者_运维问答 $(child).html(options);
});
});
}
Subcategories controller
class SubcategoriesController < ApplicationController
layout 'application'
def index
@subcategories = Subcategory.find_all_by_category_id(params[:id])
respond_to do |format|
format.js {render :json => @subcategories.collect {|sc| {:optionValue => sc.id,
:optionDisplay => sc.name} }.to_json }
end
end
def show
@subcategory = Subcategory.category_permalink_like(params[:category]).
permalink_like(params[:subcategory]).first
@with_banner = @subcategory.companies.active.with_banner
@without_banner = @subcategory.companies.active.without_banner
end
end
I'm tailing the development.log file and when I use any browser except IE the log shows
Processing SubcategoriesController#show (for 192.168.1.70 at 2010-08-26 01:49:06) [GET]
Parameters: {"id"=>"4", "_"=>"1282805337516", "show_type"=>"available_banners"}
but when I use IE I get
Processing SubcategoriesController#create (for 192.168.1.70 at 2010-08-26 01:50:09) [POST]
Parameters: {"ajax"=>"true", "authenticity_token"=>"Eg2XAvSSHg/v12cKjTPt+HkKWhxdGW3s5n6lm9jHu2A=", "id"=>"6"}
There is no defined create action, so it crashes.
I have no idea why this is interpreted differently :/
Any suggestions?
Thanks!!
Ok, this is a ridiculous fix for the problem, but I am at my wits end and this seems to work.
I modified the subcategories controller like so:
class SubcategoriesController < ApplicationController
layout 'application'
def index
@subcategories = Subcategory.find_all_by_category_id(params[:id])
respond_to do |format|
format.js {render :json => @subcategories.collect {|sc| {:optionValue => sc.id,
:optionDisplay => sc.name} }.to_json }
end
end
def show
@subcategory = Subcategory.category_permalink_like(params[:category]).
permalink_like(params[:subcategory]).first
@with_banner = @subcategory.companies.active.with_banner
@without_banner = @subcategory.companies.active.without_banner
end
#this is the added method
def create
index
end
end
for some reason, IE keeps trying to call the create
method in the subcategories controller where all the other browsers play nice and call the index
method
I really don't know why this is happening, so this hack will have to do for now :(
I've simplified the code I posed above so its easier to read.
I recall debugging an issue very similar to this recently. If I recall correctly, it has to do with the mime type & dataType passed to jquery. Look at using get, or getScript instead.
精彩评论