- form_for
<%= form_for(@article) do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%end%>
In this example i supplied an object of article to the form_for method.
It will generate the following:
<form action="/articles" class="new_article" id="new_article" method="post">
<label for="article_title">Title</label>
<input id="article_title" name="article[title]" type="text" />
<%= f.text_field :title %>
For text_field there must be an existing column in the database like in this case you must have a column name 'title' in the database.
It is always used with the combination of form_for.
<%= f.radio_button :contact_method, 'email' %>
<%= f.label :contact_method, 'Email', :value => 'email' %>
They are generally used for small selection list.
They are generally created as an independent object that are united together by a naming convention.
<%= check_box("notes", note.id, {checked: true}) %>
By default it's value is set to true which means it will be checked by default.
2. form_tag
Syntax:
<% form_tag(articles_path, :method=>'post', :multipart => true) do %>
You simply defines the path to the form_tag.
Then you define it what HTTP request to make like in this case you are making a 'post' request.
<%= text_field_tag :email %>
text_field_tag is used if we do not want to involve the database.
0 Comment(s)