2009-12-02 25 views
0

我將一些AJAX放入一個rails項目。我正嘗試用部分內容刷新div。(rails)試圖添加AJAX,但ajax返回一個巨大的嘗試{..} catch {..}塊

page.replace_html("chemicals", :partial => "chemicals", :object => @chemicals) 

但是,這將返回一個巨大的try/catch塊,而不是正確地重新渲染部分。任何想法是怎麼回事?

的錯誤是:

try {Element.update("chemicals", "<table>\n <tr>\n <th> name</th>\n <th>Cas</th>\n <th>Amount</th>\n <th>Units</th>\n <th>Vendor</th>\n\t<th>Vendor email</th>\n\t<th>Vendor website</th>\n\t<th>Vendor phone</th>\n </tr>\n\n\n\n <tr>\n <td>HCl</td>\n <td><a href=\"http://chempedia.com/registry_numbers/\"></a></td>\n <td>10</td>\n <td>mL</td>\n <td>Sigma-Aldrich</td>\n\t\n\t<td></td>\n\t<td>http://www.sigmaaldrich.com/sigma-aldrich/home.html</td>\n\t<td>800-325-3010</td>\n\t\n <td><a href=\"/chemicals/28\">Show</a></td>\n <td><a href=\"/chemicals/28/edit\">Edit</a></td>\n <td><a href=\"/chemicals/28\" onclick=\"if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'dQYsh4Kz2kIX92HT0Ove43rO+qdz+/y5aozZIcdMWeg='); f.appendChild(s);f.submit(); };return false;\">Destroy</a></td>\n </tr>\n\n</table>"); 
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"chemicals\", \"<table>\\n <tr>\\n <th> name</th>\\n <th>Cas</th>\\n <th>Amount</th>\\n <th>Units</th>\\n <th>Vendor</th>\\n\\t<th>Vendor email</th>\\n\\t<th>Vendor website</th>\\n\\t<th>Vendor phone</th>\\n </tr>\\n\\n\\n\\n <tr>\\n <td>HCl</td>\\n <td><a href=\\\"http://chempedia.com/registry_numbers/\\\"></a></td>\\n <td>10</td>\\n <td>mL</td>\\n <td>Sigma-Aldrich</td>\\n\\t\\n\\t<td></td>\\n\\t<td>http://www.sigmaaldrich.com/sigma-aldrich/home.html</td>\\n\\t<td>800-325-3010</td>\\n\\t\\n <td><a href=\\\"/chemicals/28\\\">Show</a></td>\\n <td><a href=\\\"/chemicals/28/edit\\\">Edit</a></td>\\n <td><a href=\\\"/chemicals/28\\\" onclick=\\\"if (confirm(\'Are you sure?\')) { var f = document.createElement(\'form\'); f.style.display = \'none\'; this.parentNode.appendChild(f); f.method = \'POST\'; f.action = this.href;var m = document.createElement(\'input\'); m.setAttribute(\'type\', \'hidden\'); m.setAttribute(\'name\', \'_method\'); m.setAttribute(\'value\', \'delete\'); f.appendChild(m);var s = document.createElement(\'input\'); s.setAttribute(\'type\', \'hidden\'); s.setAttribute(\'name\', \'authenticity_token\'); s.setAttribute(\'value\', \'dQYsh4Kz2kIX92HT0Ove43rO+qdz+/y5aozZIcdMWeg=\'); f.appendChild(s);f.submit(); };return false;\\\">Destroy</a></td>\\n </tr>\\n\\n</table>\");'); throw e } 

當我說回報try/catch塊,這是什麼東西被顯示在瀏覽器上。

編輯:

問題的一部分可能是而不是重定向到本地主機:3000 /控制器,我開始在按下了刪除鍵,我被重定向到本地主機:3000 /控制器/ id,其中id是我刪除的對象的id。任何想法這裏發生了什麼?

也是,正確刪除對象。它只是不會回到正確的頁面。這裏是我點擊的'刪除'鏈接和'刪除'的控制器。

<td><%= link_to 'Destroy', chemical, :confirm => 'Are you sure?', :method => :delete %></td> 

# DELETE /chemicals/1 
    # DELETE /chemicals/1.xml 
    def destroy 
    @chemical = Chemical.find(params[:id]) 
    @chemical.destroy 
    @chemicals = Chemical.all 
    respond_to do |format| 
     #format.html { redirect_to(chemicals_url) } 
     #format.xml { head :ok } 
     format.js 
    end 
    end 

(我用的腳本由化學品/生成支架化學品)

回答

3

雖然JavaScript是用來處理POST/PUT/DELETE請求,請求通過的link_to觸發仍預計HTML並將響應呈現爲收到html。

相反,你應該使用link_to_remote,它知道執行它所接收的任何東西,假設它有Javascript。這將做你所期望的:

<%= link_to_remote 'Destroy', :url => {:id => chemical.id, :action => :destroy}, 
     :confirm => 'Are you sure?', :method => :delete %></td> 

p.S.在生產模式下,返回給瀏覽器的JavaScript不會被封裝在巨大的try塊中。

+0

好的,我不再獲取JS,但現在它不會刪除化學 – sepiroth 2009-12-02 03:40:29

+0

不要緊,需要一個:action =>'destroy'在url中。謝啦!! – sepiroth 2009-12-02 04:01:45

+0

對不起,我忘了指定銷燬行動。如果:action選項被遺漏在:url散列之外,則Rails將假定當前操作(索引)。我已經解決了這個問題。 – EmFi 2009-12-02 04:02:44