简体   繁体   中英

Ruby on Rails: difference between text_area and text_area_tag helpers?

in Ruby on Rails: What is the difference between text_area and text_area_tag helpers?

More importantly, I would like to know which one is more suited to long HTML text input (specifically in my case, blog posts ) ??

Difference is that if you use form_for , pass ActiveRecord object to it and pass, let's say, f to block, it is much more convenient to use for example

<%= f.text_area :body %>

because it sets proper id, name and value automatically. There's no difference between these helpers in handling long HTML text inputs, but if you want to use it for ActiveRecord object form, you should use text_area because, as I said, it's more convenient.

There are two types of form helpers: those that specifically work with model attributes and those that don't .

Ref text_area which specifically work with model

text_area(:post, :body, :cols => 20, :rows => 40)

this will create following html

   <textarea cols="20" rows="40" id="post_body" name="post[body]">
     #{@post.body}
   </textarea>

Ref text_area_tag which doesn't rely on an Active Record objec

text_area_tag 'post'

will create following

<textarea id="post" name="post"></textarea>

text_area set tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object) text_area(:post, :body, :cols => 20, :rows => 40) generate:

<textarea cols="20" rows="40" id="post_body" name="post[body]">
  #{@post.body}
</textarea>

And text_area_tag 'post' generate

<textarea id="post" name="post"></textarea>

For more information, look: http://apidock.com/rails/ActionView/Helpers/FormTagHelper/text_area_tag http://apidock.com/rails/ActionView/Helpers/FormHelper/text_area

<%= f.text_area :attribute_name %>

<%= text_area_tag :any_name, :any_value %>

if you use form_for (always recommended) to render form then use

<%= f.text_area ....

otherwise you have to use

<%= text_area_tag ....

either will serve the same and no impact on input data (text) size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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