![](/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.