Connect With Us

Application Template in Rails

By Kuber Aaganja on Dec, 06, 2016 in application template, rails

While starting any new project, we have to install different gems, generate the root controller and set the default routes, initialize the git, add some files to gitignore and lots of other stuffs. You might have questions whose answers would ease you the problem we just discussed. Well, I had some and here are the answers.

Won’t it be great if these all could be setup while creating new application?
Do you think it is possible to generate everything while creating new application?
Yes, of course, it is.

So what do you think that made it possible?
Well, one of the ways is “Rails Application Template”.

Ok, great! Then, what’s “Rails Application Template”?
The Rails official site defines “Rails Application Template” as ‘Application templates are simple Ruby files containing DSL for adding gems/initializers etc. to your freshly created Rails project or an existing Rails project.’

Now, how to use the ‘Rails application Template’?
It is simple. What you need to do is, provide the Rails generator with the location of the template you wish to apply using the -m option. This can either be a path to a file or a URL.
i.e.

$ rails new app_name -m path_to_template_file/template.rb

OR

$ rails new app_name -m http://url_of_template_file

Example:

$ rails new app_name -m https://raw.githubusercontent.com/aaganja/app_template/master/app_template.rb

Since, the template is run in the context of application generator, we will have access to all rails generator actions, including thor at this template.

Sample of the template.rb

remove_file "README.rdoc"
create_file "README.md",  "TODO"
gem 'pry-rails', group: [:test, :development]
run "bundle install"


if yes? "Do you want to generate root controller? yes/no"
  name = ask("what should be its name?").underscore
  generate :controller, "#{name} index"
  route "root to: '#{name}\#index'"
end


git :init
append_file ".gitignore", "config/database.yml"
run "cp config/database.yml config/example_database.yml"
git add: '.', commit: "-m 'initial commit'"

Advantages using rails application template

  • Ability to mix and match templates
  • Dependencies stay up-to-date
  • Default settings or custom settings in multiple project can be reuse
  • Huge time saving in getting a project started

 

Share This:

Comments


Add a new comment

Latest Posts


First Step to Software Development: Design Thinking Process By Rasna Shakya on Nov, 03, 2017

Bourbon By Rasna Shakya on Oct, 20, 2017

Design Sprint By Neha Suwal on Aug, 14, 2017

3 years of helping startups and entrepreneurs with technology By Neha Suwal on Aug, 04, 2017

Digital Marketing for Startups By Neha Suwal on Jun, 30, 2017