2013-02-23 35 views
0

我正嘗試在手風琴中預先打開div。我正在相應的控制器中設置實例變量@expanded_section。我想使用的jQuery的UI手風琴這樣的使用再培訓局的active參數值在我的CoffeeScript文件中的assets/javascripts文件夾CoffeeScript與ERB錯誤

$(".sections").accordion({ 
    active: <%[email protected]_section%>, 
    header: "h4", 
    collapsible: true, 
    heightStyle: "content" }).sortable({ 
    axis: "y", 
    handle: "h4", 
    update: -> 
    $.post($(this).data('update'), $(this).sortable('serialize')) 
    }) 

但我收到以下錯誤:

Error: Parse error on line 60: Unexpected ',' 

(在/home/steve/dev/rails/Sur​​vey/app/assets/javascripts/surveyys.js.coffee.erb)

line 60active: <%[email protected]_section%>,

如果我做一個@expanded_section電話to_i它呈現active:0

但是,當我從控制器打印@expanded_section值到控制檯它打印正確的預期值。

請幫忙。

回答

0
assets/

一切都將被處理之前您的控制器上運行。這意味着,@expanded_section不是來自你的控制器,它將來自任何self是當資產被編譯,因此,像所有的實例變量,將在第一次訪問創建,將是nil。結果是CoffeeScript將看到:

$(".sections").accordion({ 
    active: , 

這是一個語法錯誤。

,你可以做一個簡單的事情是設置在視圖的HTML一個全局JavaScript變量:

<script type="text/javascript"> 
    window.expanded_section = <%= @expanded_section %>; 
</script> 

,然後你的資產的腳本可以看看window

$(".sections").accordion({ 
    active: window.expanded_section, 

你可能會希望將其隱藏在自定義名稱空間中以避免與現有的window屬性發生衝突,但總體結構將保持不變。那小小的window.expanded_section = ...,但如果它是每個頁面的一部分,甚至可能會進入您的佈局。

+0

如何在haml中執行window.expanded_section? – 2013-02-23 04:47:03

+0

@stevanity:對不起,不知道,不是HAML的傢伙。谷歌搜索應該有所幫助。 – 2013-02-23 04:55:56

+0

好吧np :)我用了一個javascript過濾器。無論如何。在我的js文件,我得到'window.expanded_section'就像是:( – 2013-02-23 04:57:53