Rails link_to_remote 没有渲染任何内容,为什么?

发布于 2024-09-09 08:15:08 字数 1389 浏览 4 评论 0原文

RoR新手在这里。在《使用 Rails 进行敏捷 Web 开发》第 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> 

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

RoR newbie here. Working the "play time" exercises at the end of Agile Web Dev with Rails, chapter 9. Can't get link_to_remote to generate a link for me in a partial. My store_cart_item.html.erb partial looks like this:

<% 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 + "×",
                :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>

In the browser, link_to_remote appears to be doing nothing, b/c the html output looks like this:

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

Feels like I am missing something really obvious. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-09-16 08:15:08

标签 <% exp %> 下的表达式仅被求值,而不是赋值。

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

<%= exp %>

只需将代码更改为

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

Expression under the tag <% exp %> are just evaluated, not assign the value.

And If you want to use or display the value of expression just use "=" sign like

<%= exp %>

Just change You code to

<%= link_to_remote cart_item.quantity.to_s + "×",
                :update => "cart",
                :url => {:action => "remove_from_cart", :id => cart_item.product} %>
遥远的她 2024-09-16 08:15:08

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

Use <%= link_to_remote ... %> instead of <% link_to_remote %> (assuming you are using rails 2.x or previous versions).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文