0

RoR 新手在这里。在 Agile Web Dev with Rails 第 9 章结尾处进行“游戏时间”练习。无法让 link_to_remote 部分地为我生成链接。我的 store_cart_item.html.erb 部分看起来像这样:

<% 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> 

感觉就像我错过了一些非常明显的东西。我究竟做错了什么?

4

2 回答 2

2

使用<%= link_to_remote ... %>代替<% link_to_remote %>(假设您使用的是 rails 2.x 或以前的版本)。

于 2010-07-14T06:06:53.643 回答
2

标签下的表达式<% exp %>只是被评估,而不是赋值。

如果你想使用或显示表达式的值,只需使用“=”符号

<%= exp %>

只需将您的代码更改为

<%= link_to_remote cart_item.quantity.to_s + "&times;",
                :update => "cart",
                :url => {:action => "remove_from_cart", :id => cart_item.product} %>
于 2010-07-14T06:26:19.463 回答