【问题标题】:Passing current or updated text field tag value for update button?为更新按钮传递当前或更新的文本字段标签值?
【发布时间】:2013-12-20 05:48:28
【问题描述】:

我有一个表,我正在从会话中加载值。

我正在处理更新按钮。我想将文本字段的当前值传递给控制器​​以执行更新操作。

我的代码如下所示:

<table>
  <thead>
    <tr>
      <th width="200">Name</th>
      <th width="150">Price</th>
      <th>Quantity</th>
      <th width="150">Total</th>
      <th></th>
    </tr>
    <% grand_total = 0 %>
    <% session[:cart].each do |key, item| %>
    <tr>
      <td><%= item[:name] %></td>
      <td>Rs. <%= item[:price] %></td>
      <td><%= text_field_tag(:quantity, item[:quantity]) %>
        <%= link_to("Update", {:action => "update", :id => item[:id], :quantity => item[:quantity]}) %>
      </td>
      <td><%= item[:total_cost] %></td>
      <td>
        <%= link_to("X", {:action => "delete", :id => item[:id]}) %>
      </td>
    </tr>
    <% grand_total = grand_total + item[:total_cost] %>
    <% end %>
  </thead>
</table>

当我将鼠标悬停在更新按钮上时,即使我更改了文本字段中的值,路径也会显示“localhost:3000/cart?quantity=1”。

【问题讨论】:

    标签: javascript html ruby-on-rails ruby erb


    【解决方案1】:

    ERB 模板由 Rails 在服务器端呈现,但这种交互发生在客户端,在浏览器中。

    &lt;%= ... %&gt; 是一种奇特的字符串插值形式,但您仍然可以以同样的方式思考它:

    quantity = 1
    message = "You have #{quantity} items"
    quantity = 2
    puts message # -> You have 1 items
    

    你不会真的期望它会打印You have 2 items,对吗?

    您最简单的选择是在将更新提交按钮的文本输入上的onchange 指令中放置一些javascript,但如果您经常遇到此问题,您可能还想查看“数据绑定”。

    【讨论】:

    • 单独提问;在此处放一个链接,稍后再回来删除评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 2015-09-03
    • 2020-04-08
    • 1970-01-01
    相关资源
    最近更新 更多