简体   繁体   English

将变量从视图传递到控制器Rails 4

[英]Passing variable from view to controller Rails 4

I want to pass one variable which is introduced by the user in the front-end. 我想传递一个由用户在前端引入的变量。 I dont use any model (i dont need it, because im working with JSON data all in memory). 我不使用任何模型(我不需要它,因为我在内存中都使用JSON数据)。 I have looked many tutoriasl but almost all of them are focused on filling out a form. 我看了很多tutoriasl,但是几乎所有的tutoriasl都专注于填写表格。 My application does not have any form nor tables. 我的应用程序没有任何形式或表格。 Any idea? 任何想法? Thank you. 谢谢。

{<div class="module1">
    <p>Mein Lastprofil berechnen</p>
    <div class="boxed">
      Jahreshausverbrauch (kWh)
      <%= text_field_tag "input", nil, placeholder: "3500" %>
      <%= button_to "Senden", root_path, :method => :get %>
     </div> 
</div>}

I want to save/pass the variable introduced when clickling the button. 我想保存/传递单击按钮时引入的变量。

By having a text field you implicitly already have a form. 通过具有文本字段,您隐式地已经具有一个表单。

Make it explicit by wrapping the text field and button: 通过包装文本字段和按钮使其明确:

<%= form_tag root_path, method: :get do %>
  <%= text_field_tag :input, placeholder: "3500" %>
  <%= submit_tag "Senden" %>
<% end %>

Then you can access the value as params[:input] in the controller. 然后,您可以在控制器中以params[:input]访问该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM