简体   繁体   English

form_for 中 text_field 和 text_area 的区别

[英]difference between text_field and text_area in form_for

So I have seen examples of text_field and text_area being used in forms like this:所以我看到了 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 %> 

I don't understand the difference, though.不过,我不明白其中的区别。 Is it necessary for a beginner Rails developer to understand the difference?初学者 Rails 开发人员是否有必要了解其中的区别?

I found some explanations in the API which I don't understand - perhaps somebody can take a look and let me know what is going on.我在 API 中发现了一些我不明白的解释 - 也许有人可以看看并让我知道发生了什么。

For "text_area":对于“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.

Then, for "text_field":然后,对于“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.

a.text_field :name is parse to the following html a.text_field :name解析为以下 html

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

a.text_area :name would parse to something like: a.text_area :name会解析为:

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

</textarea>

depending on the options passed.取决于通过的选项。

The simplest way of looking at it is text_field gives you a place for a single line of text, where text_area gives an area for multiple lines.最简单的查看方式是text_field为单行文本提供一个位置,其中 text_area 为多行提供一个区域。

you can pass a hash of options to the text_area helper to specify the number of rows and columns.您可以将选项的散列传递给text_area助手以指定行数和列数。

In the example you give above, it would be poor practice to use either text_field or text_area for passwords, you'd be better to use a.password_field在你上面给出的例子中,使用text_fieldtext_area作为密码是不好的做法,你最好使用a.password_field

thats a good answer - funny when googling this and looking for a spot on example - after i read the response above - i looked at my code and realized it was an even better example这是一个很好的答案 - 在谷歌搜索并寻找示例中的位置时很有趣 - 在我阅读上面的回复之后 - 我查看了我的代码并意识到这是一个更好的示例

 <%= f.label :name %>
 <%= f.text_field :name %><br />
 <%= f.label :bio %>
 <%= f.text_area :bio %><br />

makes sense, name would only need a single line (unless you have a super long name) where as bio, would need multiple lines.有道理, name 只需要一行(除非你有一个超长的名字),而 bio 则需要多行。

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

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