我完全被这个困住了,几个小时的搜索还没有出现结果。当我尝试执行 ajax 请求时出现 406 错误。这是我的代码..
层控制器
def index
series = Series.find_by_id(params[:series_id])
respond_to do |format|
format.json { render :json => series.tiers }
end
end
Javascript
$('#series_id').change ->
series_id = $('#series_id').val()
$.get '/admin/tiers', {series_id:series_id}, (result) ->
element = $('select[name *= "tier_id"]')
element.empty()
$.each result, (index, item) ->
element.append $('<option/>', value:item.id, text: item.title)
这是我从 Rails 服务器得到的错误
Started GET "/admin/tiers/1" for 127.0.0.1 at 2013-01-21 14:19:38 +1100
Processing by Admin::TiersController#index as JSON
Parameters: {"series_id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.1ms) begin transaction
(0.5ms) UPDATE "users" SET "last_request_at" = '2013-01-21 14:19:38.827810', "perishable_token" = 'y0TMicsW2l4zgSiQcJx', "updated_at" = '2013-01-21 14:19:38.829452' WHERE "users"."id" = 1
(2.5ms) commit transaction
Tier Load (0.3ms) SELECT "tiers".* FROM "tiers" WHERE "tiers"."id" IS NULL LIMIT 1
Tier Load (0.3ms) SELECT "tiers".* FROM "tiers"
Series Load (0.2ms) SELECT "series".* FROM "series"
Series Load (0.2ms) SELECT "series".* FROM "series" WHERE "series"."id" = 1 LIMIT 1
Completed 406 Not Acceptable in 30ms (ActiveRecord: 4.4ms)
真的不知道在塞什么东西,控制器看起来不错,看起来内容类型是 json。
我也在使用 rails 3.2.11