【问题标题】:Rails erb get value of a text_field_tagRails erb 获取 text_field_tag 的值
【发布时间】:2016-11-07 18:33:08
【问题描述】:

我有如下简单的 html.erb 表单

<table>
  <%= text_field_tag :tesintg %>
  <th><%= button_to 'Apply now', apply_product_index_path(:value => "Want Value of input in text field here") , method: :post %></th>
</table>

按下“立即应用”按钮时,我希望将测试 text_field_tag 中的值作为查询参数发布为 {"value" : "Value in the text field"}

我如何做到这一点。

【问题讨论】:

  • 是否有某些原因您不能将这些元素包装在一个表单中并让它为您工作?那将是最简单的解决方案。否则,您将需要使用 JavaScript。
  • 是的,使用传统的 html 表单更容易。或者,您可以使用 javascript 从文本字段中获取值并将其注入查询参数。
  • 肖恩如何做到这一点。如何使用 javascript 获取值并在 button_to 行中使用它。

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


【解决方案1】:

我认为最好的方法就是创建form_tag

<%= form_tag apply_product_index_path, method: :post do %>
   <%= text_field_tag :teasing %>
   <%= submit_tag %>
<% end %>

这将传递给您的控制器哈希params: { teasing: 'value passed as teasing }。您可以通过params[:teasing] 轻松使用它。

您无需从text_field_tag 中获取价值并将其放入button

还请记住,如果您正在创建新对象,通常首选的方法是使用使用特定模型的form_for 标签。我不确定你的意图是什么,所以我不会重写已经说过的所有内容。你可以在这里阅读更多内容:http://guides.rubyonrails.org/form_helpers.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多