Why we have chosen simple_form :
– simple_form is lightweight
– It comes up with basic HTML mark up giving freedom to customize the CSS at your own comfort.
Build a basic application:
$ rails new DemoApp $ rails g scaffold blog title:string description:text $ rake db:migrate Now check app/blogs/_form.html.erb : <%= form_for(@blog) do |f| %> <div class="field"> <%= f.label :title %> <br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :description %><br /> <%= f.text_area :description %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
Setup simple_form gem for our application:
Add gem ‘simple_form’ to our Gem file and run bundle install.
$ rails g simple_form:install
Now we need to run generator that will add few necessary files to our application. (As shown in image)
Now modify our _form.html.erb, replace form_for with simple_form_for and add f.input for each of the input fields.
<%= simple_form_for(@blog) do |f| %> <%= f.input :title %> <%= f.input :description %> <%= f.button :submit %> <% end %>
Now open link http://localhost:3000/ (Don’t forget to start rails server & I am assuming blogs#new has been set as your root page in routes.rb ) You will get a blog form with Title & Description field as demonstrated below :
Go through rdoc documentation of simple_form for applying more customization on each fields. (date field, combo box, choice box, dropdowns)