![](/img/trans.png)
[英]How do input field methods (text_area, text_field, etc.) get attribute values from a record within a form_for block?
[英]difference between text_field and text_area in form_for
所以我看到了 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 %>
不过,我不明白其中的区别。 初学者 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.
然后,对于“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
解析为以下 html
<input type="text" name="name">
a.text_area :name
会解析为:
<textarea rows="4" cols="50">
</textarea>
取决于通过的选项。
最简单的查看方式是text_field
为单行文本提供一个位置,其中 text_area 为多行提供一个区域。
您可以将选项的散列传递给text_area
助手以指定行数和列数。
在你上面给出的例子中,使用text_field
或text_area
作为密码是不好的做法,你最好使用a.password_field
这是一个很好的答案 - 在谷歌搜索并寻找示例中的位置时很有趣 - 在我阅读上面的回复之后 - 我查看了我的代码并意识到这是一个更好的示例
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :bio %>
<%= f.text_area :bio %><br />
有道理, name 只需要一行(除非你有一个超长的名字),而 bio 则需要多行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.