difference between text_field and text_area in form_for - ruby-on-rails

So I have seen examples of text_field and text_area being used in forms like this:
<%= 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?
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.
For "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(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
<input type="text" name="name">
a.text_area :name would parse to something like:
<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.
you can pass a hash of options to the text_area helper to specify the number of rows and columns.
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

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.

Related

form for helper bad collection

I am useing a form_for helper to collect data on the client side of my application. However something weird is happening. I am not collecting the :name and :description and they are both returning as nil. this is my code:
<%= form_for #type do |f| %>
....
<%= f.text_field :name, :class => "col-xs-4" %>
<%= f.text_field :description, :class => "col-xs-4" %>
<%= f.submit %>
....
Do I need to make a fields_for under the form_for to get this working? It is a bit tricky because I am using #type which in this case is set up to tell the view which kind of attr. they are looking at. For example, this line:
<%= f.label #type %> <label> Description</label>
depending on what view you are on shows ether:
Group Description
or
Tag Description
and because they are both technically the same, I am using the same index for both. I hope I am clear with my issue and thank anyone who understands my problem and solution.
The param name will depend on the object you pass.
If #type contains an instance of Group, then you will get the params under params[:group], and if it is an instance of Tag, the you will get them on params[:tag]
<%= form_for #type do |f| %>
<%= f.label :name, "#{#type.model_name} Description" %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
Note the label definition. The way you are defining it will create 2 labels and the second one will not be linked to any field.
fields_for is normally used when you are creating several objects within the same form, for instance a Project and several tasks associated to it.
Hope this helps.
update:
If #type is a string or symbol it should work too. The tradeoffs using this approach will be that if there are any validation errors when creating the object, those will not be displayed and the fields will not be prefilled with the input that the user gave before submitting the form, forcing the user to enter all the information again and guessing which was the validation error (you can initialize it from the received params, but that complicates the code readability)
The unique thing different in your view would be the label definition.
<%= f.label :name, "#{#type} Description" %>

Ruby on rails cant get values from textfield to array

I need to get an array of strings from f.text_field and store it into a database.
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :tags %>
<%= f.text_field :tags%>
</div>
tags is an attribute and it is in an array. Name stores into the database correctly but nothing to tags.
How can i do this?
Thanks
You'll probably want to either parse that on the client side with JS to create an array that gets stored in a hidden field, or something similar. Alternatively, you should pass the raw text to the controller and parse it ruby. You should probably do the latter as you will probably want to sanitize the input.
There is a very good gem available for handling tagging. It's called acts-as-taggable and is found here https://github.com/mbleigh/acts-as-taggable-on

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

How can I use token fields with acts_as_taggable_on and formtastic?

I would like to be able to use the token fields (found here: http://railscasts.com/episodes/258-token-fields) with formtastic to allow users to input tags (I am using acts_as_taggable_on).
Can someone walk me through an example of how to do this?
I don't have the time for a full walk-through, but the guts of this seems to be that a text_field has a data-pre attribute with some JSON in it. The way to add attributes to inputs in Formtastic is via the :input_html option, so this:
<p>
<%= f.label :author_tokens, "Authors" %><br />
<%= f.text_field :author_tokens, "data-pre" => #book.authors.map(&:attributes).to_json %>
</p>
Might be translated to something like:
<%= f.input :author_tokens, :input_html => { "data-pre" => #book.authors.map(&:attributes).to_json } %>
The rest is gluing CSS to HTML and finding the right DOM element to apply the jQuery to (view source is your friend).

rails check_box_tag value is NULL

I am not sure why I am having this problem, maybe I am using the check_box_tag incorrectly.
I have a form that is used to send an email message. You are supposed to be able to choose one or more boxes that represent different groups of people.
<%= check_box_tag (:bcc_email, value = #spouses) %> <%= f.label :bcc_email, "Company Spouses" %><br />
<%= check_box_tag (:bcc_email, value = #soldiers) %> <%= f.label :bcc_email, "Company Soldiers" %><br />
The values are an array of email addresses. Those work fine, I have had them functioning as drop down menus for sometime.
When I look at the HTML page source the values are there, they are just not being passed along with the create method.
Any ideas?
Ah of course I figure the problem out right as I post this. For anyone out there that makes really obvious mistakes like me:
Since I am using a check_box_tag, I never specified an object. Should look like this:
<%= check_box_tag ('message[bcc_email]', value = #spouses) %> <%= f.label :bcc_email, "Company Spouses" %><br />

Resources