We got many gems to customize our form in rails out of which we will be using simple_form.
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 :
Setup simple_form gem for our application: Add gem ‘simple_form’ to our Gemfile 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.
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) !