Is there a good API Doc for Rails Form Helper? - ruby-on-rails

I'm new on ROR and
making some front-end html codes to rails form helper codes.
For example, this is the html code,
<input id="username" type="text" placeholder="Username" autofocus required>
If i want to make this to rails form helper code it will be like this,
<%=form.text_field :input, :placeholder => 'Username', :autofocus=>true%>
The problem is, it is really inconvenient translating from html code to rails code.
I don't know the attributes for the rails code, so i had to google all the attributes one by one(ex> rails form.text_field autofocus required) and this is making me developing really slow.
Is there are good api site like w3schools for checking the whole attributes or option inside here?

Both links below are from official ruby on rails site
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
http://guides.rubyonrails.org/form_helpers.html

if you simply want to make same input field you need to use this syntax
<%= text_field_tag :username, :placeholder => "Username", :autofocus => true %>
If you use form.text_field in that case rails generate html tag with your object's class for which you have created form_for like:
<% form_for Blog.new do |form| %>
<%= form.text_field :username, :placeholder => 'Username', :autofocus=>true %>
<% end %>
It will generate like this:
<input id="blog_username" name="blog[username]" type="text" placeholder="Username" autofocus required>

Apart from the above ones, this one is also good for the starters
http://www.tutorialspoint.com/ruby-on-rails/rails-html-forms.htm

Related

rails: text_field attributes in lowercase

I have the following text_field:
<%= f.text_field( :with_created_at_gte, class: 'form-control datepicker filterrific-periodically-observed', 'data-provide': 'datepicker', "data-date-clearBtn": true ) %>
Which outputs:
<input class="form-control datepicker filterrific-periodically-observed" data-provide="datepicker" data-date-clearbtn="true" type="text" value="05/08/2015" name="filterrific[with_created_at_gte]" id="filterrific_with_created_at_gte">
The problem at hand is: my attribute is data-date-clearBtn, but Rails renders data-date-clearbtn (lowercase b). How can I avoid this problem? I also tried:
<%= f.text_field( :with_created_at_gte, class: 'form-control datepicker filterrific-periodically-observed', data: { 'provide': 'datepicker', "date-clearBtn": true } ) %>
Consider that html tags and attributes are case insensitive, check the comments in this answer, so Rails is actually correct in this situation.
I don't think you can overwrite that behavior directly, you should either write your helper or type the html tag manually. The rails helper is designed in that way

How Can I Embed Ruby into Bootstrap Input Form?

I want to put this: <%= f.text_field :name %>
into the code below, but I keep getting an error. How can I properly embed it so that the code will work. Thanks!
<form>
<div class="form-group">
<input type= class="form-control" id="exampleInputEmail1" placeholder="Enter Value">
</div>
</form>
You dont' need the whole input section, just do this :
<%= f.text_field :name , :class=> "form-control"%>
Just add bootstrap classes into your code via parameter :class
Use Formtastic Bootstrap it's a gem for rails, create forms with automatic bootstrap class
'f' is not instantiated in you code so following code will raise error in the form-
<%= f.text_field :name %>
alternatively you can use this -
<%= text_field_tag :name,'', :placeholder => "Enter Value", :class=>"form-control", :id=>"exampleInputEmail1" %>
this will not raise any error on embedding in the form.

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

rails, adding an undefined attribute to form_tag

i was wondering how do you put a "data-provide" attribute inside a rails form_tag? the html output i would like is...
<input type="text" data-provide="typeahead">
however in a form_tag, i can't just do something like
<%= f.text_field :data-provide => "typehead" %>
how can i add an undefined attribute like that in a form tag? do i need to submit a hidden field or use another helper method? i was looking through the form_tag helper api and it doesn't seem like i can just define another attribute.
it seems like there an easy solution to this, but im not quite sure. help would be appreciated. thanks
Use :data option with a hash:
<%= f.text_field :some_field, :data => {:provide => "typeahead"} %>

How to set title for text_field?

I know that is something simple, but I'm stuck.
I have a several forms in my rails3 app to which I need to add tooltips, by using the tipsy. The text in tooltips should be determined in title\original-title, but I just can't figure it out, how to set it for text_field.
<%= f.text_field :what %>
gives me
<input id="name_which" name="name[which]" size="30" type="text">
and I just can't figure it out, where to put the title text.
For example, this one in html works fine
<input type="text" name="tooltipform" title="Tooltip text">
In application.js it is determined by
$(window).load(function() {
$('#form-example [title]').tipsy({trigger: 'focus', gravity: 'w'});
});
I know that is something basic and I'm ashamed of my self. Thank you in advance.
Try with:
<%= f.text_field :what, :title => 'whatever' %>
It's quite simple, pass it as an option:
<%= f.text_field :what, :title => "Tooltip text %>
Other html attributes can also be set as this, such as :class, :id, etc.

Resources