2014-04-28 14 views
0

我按照這個教程定義http://pullmonkey.com/2008/03/30/dynamic-select-boxes-ruby-on-rails/ ,也從未捕獲的ReferenceError:Ajax不是平變化問題

這裏整合highchart例子是控制器:

class TestItController < ApplicationController 
    def index 
    @genres = Genre.find(:all) 
    @artists = Artist.find(:all) 
    @songs = Song.find(:all) 
    end 

    def update_artists 
    # updates artists and songs based on genre selected 
    genre = Genre.find(params[:genre_id]) 
    artists = genre.artists 
    songs = genre.songs 

    render :update do |page| 
    page.replace_html 'artists', :partial => 'artists', :object => artists 
    page.replace_html 'songs', :partial => 'songs', :object => songs 
    end 
    end 

    def update_songs 
    # updates songs based on artist selected 
    artist = Artist.find(params[:artist_id]) 
    songs = artist.songs 

    render :update do |page| 
    page.replace_html 'songs', :partial => 'songs', :object => songs 
    end 
    end 
end 

以下機型:

class Genre < ActiveRecord::Base 
    has_many :artists 
    has_many :songs, :through => :artists 
end 
class Artist < ActiveRecord::Base 
    has_many :songs 
end 

以下是視圖:index.html.erb

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="http://code.highcharts.com/highcharts.js"></script> 

<div id="contain" style="min-width: 310px; height: 400px; margin: 0 auto"></div> 

<script type="text/javascript"> 
$(function() { 
$('#contain').highcharts({ 
    chart: { 
     plotBackgroundColor: null, 
     plotBorderWidth: null, 
     plotShadow: false 
    }, 
    title: { 
     text: 'Browser market shares at a specific website, 2014' 
    }, 
    tooltip: { 
     pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' 
    }, 
    plotOptions: { 
     pie: { 
      allowPointSelect: true, 
      cursor: 'pointer', 
      dataLabels: { 
       enabled: true, 
       format: '<b>{point.name}</b>: {point.percentage:.1f} %', 
       style: { 
        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' 
       } 
      } 
     } 
    }, 
    series: [{ 
     type: 'pie', 
     name: 'Browser share', 
     data: [ 
      ['Firefox', 45.0], 
      ['IE',  26.8], 
      { 
       name: 'Chrome', 
       y: 12.8, 
       sliced: true, 
       selected: true 
      }, 
      ['Safari', 8.5], 
      ['Opera',  6.2], 
      ['Others', 0.7] 
     ] 
    }] 
}); 
}); 
</script>  



<%= collection_select(nil, :genre_id, @genres, :id, :name, 
        {:prompt => "Select a Genre"}, 
        {:onchange => "#{remote_function(:url => {:action => "update_artists"}, 
                :with => "'genre_id='+value")}"}) %> 

<div id="artists"><%= render :partial => 'artists', :object => @artists %></div> 
<div id="songs"><%= render :partial => 'songs', :object => @songs %></div> 

的_artists部分(_artists.html.erb):

<%= collection_select(nil, :artist_id, artists, :id, :name, 
       {:prompt => "Select an Artist"}, 
       {:onchange => "#{remote_function(:url => {:action => "update_songs"}, 
                :with => "'artist_id='+value")}"}) %> 

的_songs部分(_songs.html.erb):

<%= collection_select(nil, :song_id, songs, :id, :title, 
       {:prompt => "Select a Song"}) %> 

我得到這個錯誤時選擇Gnre:

Uncaught ReferenceError: Ajax is not defined 
    onchange 

請有人可以幫我這個:

我花了3個月,仍然沒有找到答案。

似乎是jquery.min和組合框事件onchange之間的衝突。

+0

嗨,你的佈局文件是什麼樣子?它是否包含'prototype' libraray?您將需要它,因爲Rails 2使用'prototype'作爲默認的javascript libraray和'remote_fuinction'助手使用原型JavaScript代碼。 – rubyprince

+0

Rubyprince,我看到在這裏另一個人有相同的錯誤,不能使用高圖的原型http://stackoverflow.com/questions/15074474/highchart-with-prototype –

回答

0

把你的jQuery代碼在這一行$(document).on('ready', function(){})片段,這也許有問題,「Ajax function not defined」是範圍或malform jQuery的

1

阿賈克斯在prototypejs定義的問題。您應該在使用Ajax的代碼之前包含原型腳本。 http://prototypejs.org/

進一步閱讀herehere

相關問題