form_for中text_field和text_area之间的区别

max*_*ner 5 ruby-on-rails

所以我看到text_field和text_area的例子在这样的表格中使用:

<%= form_for :account do |a| %>
    Name: <%= a.text_field :name %><br />
    Password: <%= a.text_area :password %><br />
    Password Confirmation: <%= a.text_field :password_confirmation %><br />
<%= a.submit %>
<% end %> 
Run Code Online (Sandbox Code Playgroud)

但我不明白其中的区别.初学者Rails开发人员是否有必要了解其中的差异?

我在API中找到了一些我不明白的解释 - 也许有人可以看看,让我知道发生了什么.

对于"text_area":

text_area(object_name, method, options = {})

Returns a textarea opening and closing tag set tailored for accessing a 
specified attribute (identified by method) on an object assigned to the template 
(identified by object). 
 Additional options on the input tag can be passed as a hash with options.
Run Code Online (Sandbox Code Playgroud)

然后,对于"text_field":

  text_field(object_name, method, options = {}) Link

    Returns an input tag of the “text” type tailored for accessing a specified 
attribute (identified by method) on an object assigned to the template 
(identified by object). Additional options on the input tag can be passed 
as a hash with options. These options will be tagged onto the HTML as an 
HTML element attribute as in the example shown.
Run Code Online (Sandbox Code Playgroud)

mut*_*amb 7

a.text_field :name 解析以下html

<input type="text" name="name">

a.text_area :name 会解析类似的东西:

<textarea rows="4" cols="50">

</textarea>
Run Code Online (Sandbox Code Playgroud)

取决于传递的选项.

查看它的最简单方法是text_field为单行文本提供一个位置,其中text_area为多行提供一个区域.

您可以将选项的哈希值传递给text_area帮助程序以指定行数和列数.

在上面给出的示例中,使用text_fieldtext_area使用密码是不好的做法,您最好使用a.password_field