2011-05-08 116 views
3

我有下面的代碼一個Rails應用程序在我的意見之一:的Rails:確認不顯示對話框

<% if @present.taken == false %> 
    <%= link_to "I want to buy this present", :confirm => 'Are you sure you want to buy this present?', :action => "taken_toggle", :id => @present.id %> 
<% end %> 

但是,我沒有得到一個JavaScript對話框顯示 - 它只是似乎跳過那一點(動作的調用起作用)。

我的應用程序佈局有以下幾點:

<%= stylesheet_link_tag :all %> 
    <%= javascript_include_tag :defaults %> 
    <%= csrf_meta_tag %> 

所以我想我有加載必要的JavaScript。

任何想法爲什麼這不起作用?

回答

8

由於文檔顯示

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

使用的是舊的說法風格時要小心,因爲需要額外的文字哈希:

使用嘗試像這

<% if @present.taken == false %> 
    <%= link_to "I want to buy this present", { :action => "taken_toggle"}, :confirm => 'Are you sure you want to buy this present?', :id => @present.id %> 
<% end %> 
+1

作爲旁註,請考慮使用'link_to_if'讓您的代碼更輕:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_if – apneadiving 2011-05-08 12:58:36

+1

'link_to_if'不是實際上也是一樣的,儘管如果'@present.taken == false'文本仍然會在那裏。它只是不會是一個錨標籤。 – 2013-03-20 11:24:22