Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在RubyonRails中使用表单保存文本字段的输入_Jquery_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4 - Fatal编程技术网

Jquery 如何在RubyonRails中使用表单保存文本字段的输入

Jquery 如何在RubyonRails中使用表单保存文本字段的输入,jquery,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Jquery,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我需要保存用户的输入,但我发现一个代码的格式很旧很糟糕 文件:public\input.html,添加以下代码: <html> <head> <title>Using Text Fields</title> </head> <body> <h1>Working With Text Fields</h1> This Ruby on Rails application

我需要保存用户的输入,但我发现一个代码的格式很旧很糟糕

文件:public\input.html,添加以下代码:

<html>
  <head>
    <title>Using Text Fields</title>
  </head>
  <body>
    <h1>Working With Text Fields</h1>
    This Ruby on Rails application lets you read data from text fields.
    <br>
    <form action = "/hello/there" >
      Please enter your name.
      <br>
      <input type="text" name="text1">
      <br>
      <br>
      <input type="submit"/>
    </form>
  </body>
</html>
文件:app\controllers\hello\u controller.rb:

class HelloController < ApplicationController
  def there
    @data = params[:text1]
  end
end
文件:app\views\hello\there.rhtml:

<html>
  <head>
    <title>Reading data from text fields</title>
  </head>
  <body>
    <h1>Reading data from text fields</h1>
    This Ruby on Rails application reads data from text fields.
    <br>
    <br>
    Your name is <%= @data %>.
    <br>
    <br>
  </body>
</html>

Start the WEBrick server: ruby script/server
Navigate to http://localhost:3000/input.html
这就是代码片段,老实说,它工作起来很有魅力,但在当前的rails api中,它给了我类似的东西:

<!--
<form action = "home/omg" >

  <%= label_tag(:text1, "Enter the message:") %>
  <%= text_field_tag :text1 %>
  <%= submit_tag"submit" %>
-->
那么有什么区别,我如何使用给定的输入


我想要和需要的是,当用户单击按钮-提交按钮-获取文本字段中的文本,并将其用作我的家庭控制器中的字符串。我怎样才能做到这一点呢?

唯一的区别是Rails指南中的代码使用的是。表单帮助程序将在服务器上进行评估,结果将发送到客户端,与第一个示例中的结果几乎完全相同。尝试使用表单帮助程序,然后在浏览器中导航到应用程序。右键单击以检查元素,您将看到标准标记,类似于您编写的内容

至于使用给定的输入,有什么东西目前不起作用吗?我还没有测试过你的代码,但看起来应该可以。如果字符串没有显示在那里。rhtml,可以解释发生了什么吗?

像建议的那样,查看Rails指南以了解form_标记是如何工作的

在你的app/views/hello/edit.erb.html中,或者在你有表单的地方,你的表单不会在public/*.html中工作,因为这些是静态资源。在输出HTML的嵌入式RuBy文件中使用form_标记,如:

将输出HTML:

请输入您的姓名 我注意到这个示例使用了.rhtml而不是ERB.ERB.html,它与之类似,但已经过时,在Rails 3+中不起作用,我敢肯定。