2010-07-14 81 views
0

RoR新手在這裏。在Agile Web Dev with Rails的第9章結束時使用「play time」練習。無法獲取link_to_remote以在部分中爲我生成鏈接。我store_cart_item.html.erb部分看起來像這樣:Rails link_to_remote無法渲染,爲什麼?

<% if cart_item == @current_item then %> 
    <tr id="current_item"> 
<% else %> 
    <tr> 
<% end %> 
    <td> 
<!-- stuck here, trying to get a link to decrement the quantity, but it doesn't 
    even show a link, I also tried nesting a link_to in form_remote_tag which 
    at least displayed link but didn't call the remove_from_cart action --> 
<% link_to_remote cart_item.quantity.to_s + "&times;", 
       :update => "cart", 
       :url => {:action => "remove_from_cart", :id => cart_item.product} %> 
</td> 
<td><%= h cart_item.title %></td> 
<td class="item-price"><%= number_to_currency(cart_item.price) %></td> 
</tr> 

在瀏覽器中,link_to_remote似乎無所事事,B/C HTML輸出如下:

<tr> 
    <td> 
    <!-- stuck here, trying to get a stupid link to decrement the quantity, doesn't even show link 
    also tried nesting it in form_remote_tag which at least displayed link but didn't call the 
    remove_from_cart action --> 
</td> 
<td>Agile Web Development with Rails</td> 
<td class="item-price">$68.85</td> 
</tr> 

感覺就像我錯過了非常明顯的事情我究竟做錯了什麼?

回答

2

標籤<% exp %>下的表達式僅被評估,不分配值。

,如果你想使用或顯示錶達式的值只使用「=」號像

<%= exp %> 

你的代碼只是改變

<%= link_to_remote cart_item.quantity.to_s + "&times;", 
       :update => "cart", 
       :url => {:action => "remove_from_cart", :id => cart_item.product} %> 
+0

哦,我覺得像這樣的混日子。謝謝您的幫助。 – Marvin 2010-07-17 22:24:54

2

使用<%= link_to_remote ... %>而不是<% link_to_remote %>(假設您使用的是rails 2.x或以前的版本)。