View Basics
If there is no action method, Rails finds template file directly
View Helper for form
- form_for
- label
- text_field
- password_field
- text_area
- file_field
- check_box
- radio_button
- select
- hidden_field
- email_field
- number_field
- range_field
- search_field
- telephone_field
- url_field
- color_field
- date_field
- datetime_field
- time_field
- month_field
- week_field
- submit
- fields_for
form_tag
No model type form
<%= form_tag({ controller: :keyword, action: :search}, id: :fm, class: :search) do %> <%= text_field_tag :keywd, '', size: 30 %> <%= submit_tag 'Search' %> <% end %>
Other example(use path and method
<%= form_tag("/admin/packs", method: "post") do %> <div class="form-group"> <%= select_tag :book, options_from_collection_for_select(@books, "id", "cname"), {:prompt => "--select books--"} %> </div> <div class="form-group"> English Name : <%= text_field_tag :ename, '', size: 30 %> </div> <div class="form-group"> Chinese Name : <%= text_field_tag :cname, '', size: 30 %> </div> <div class="form-group"> <%= submit_tag 'Create', class: 'btn, btn-primary' %> </div> <% end %>
form_for
Textbox
text_field
<%= f.text_field :isbn, size:20, maxlength: 25, readonly: true %>
Password
<%= f.password_field :isbn, size: 10, maxlength: 15, disabled: true %>
Textarea
<%= f.text_area :isbn, cols: 40, rows: 10 %>
Label
<%= f.label :title, 'Title' %> <%= f.text_field :title %>
submit
<%= f.submit 'Save', data: ( confirm: 'Do you want to save?', disable_with: 'Process...') %>
Radio
radio_button
<% f.radio_button :publish, 'A', class: :rd %> <% f.radio_button :publish, 'B', class: :rd %> <% f.radio_button :publish, 'C', class: :rd %>
Checkbox
check_box
<% f.check_box :cd, {class: 'chk'}, 'yes' 'no'%>Really?
File input
file_field
<%= f.file_field :img, size: 10, maxlength: 15 %>
Hidden
hidden_field
<%= f.hidden_field :title %>
Select
Using parameter(array)
<%= select_tag :book, options_from_collection_for_select(@books, "id", "cname"), {:prompt => "--select books--"} %>
prompt is non select message
HTML5
- color_field
- datetime_field
- email_field
- number_field
- search_field
- time_field
- week_field
- date_field
- datetime_local_field
- range_field
- telaphone_field
- url_field
<%= f.form_for(@book) do |f| %> <%= f.color_field :color %> <%= f.date_field :start_date %> <%= f.time_field :published %> <%= f.datetime_field :published %> <%= f.datetime_local_field :published %> <%= f.month_field :month %> <%= f.week_field :week %> <%= f.email_field :email %> <%= f.number_field :money %> <%= f.range_field :price, min: 0, max: 10000 step: 10 %> <%= f.search_field :search, size: 10, maxlength: 15 %> <%= f.telephone_field :telephone, size: 15, maxlength: 20 %> <% end %>
Select
select
<% f.select publish, ['AKB,'SKE','NMB'], {include_blank: 'Select me!'}, class: 'pub' %>
collection_select
Create select using database data
select info is generated by controller
view_controller.rb
def col_select @book = Book.new(publish: 'A') @books = Book.select(:publish).distinct end
view
<% form_for(@book) do |f| %> <%= f.collection_select :publish, @books, :publish, :publish %> <% end %>