Rails link_to_remote什么都不渲染,为什么?

时间:2010-07-14 05:41:21

标签: ruby-on-rails ruby link-to-remote

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> 

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

仅评估标记<% exp %>下的表达式,而不是赋值。

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

<%= exp %>

只需将您的代码更改为

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