{"id":3672,"date":"2015-04-08T20:47:18","date_gmt":"2015-04-09T00:47:18","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3672"},"modified":"2015-05-05T21:36:10","modified_gmt":"2015-05-06T01:36:10","slug":"ruby-on-rails-introduction-to-ruby-on-rails","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/ruby-on-rails-introduction-to-ruby-on-rails\/","title":{"rendered":"Ruby on Rails: Introduction to Ruby on Rails"},"content":{"rendered":"<ul>\n<li><span class=\"code\">rails\u00a0-v<\/span> to check version<\/li>\n<li><span style=\"line-height: 1.5;\">Rails takes MVC further: when you develop <strong>in Rails, you start with a working application<\/strong>, there\u2019s a place for each piece of code, and all the pieces of your application interact in a standard way<\/span><\/li>\n<li>Once you create an application with a specific version of Rails, it will continue to use that version of Rails, even if newer versions are installed on the system, until you decide it is time to upgrade (stored in the Gemfile at the project root)<\/li>\n<li>All Rails applications have <strong>testing support baked right in<\/strong><\/li>\n<li>As you add functionality to the code, Rails automatically creates test stubs for that functionality<\/li>\n<li>Rails programs are shorter and more readable due to DRY and convention over cofiguration<\/li>\n<li>Support for Ajax and RESTful interfaces is built in<\/li>\n<li>Deploy or roll-back with a single command<\/li>\n<li><strong>Rails documentation<\/strong>: If you install Rails using RubyGems, simply start the gem documentation server (<span class=\"code\">gem server<\/span>), and you can access all the Rails APIs by pointing your browser at http:\/\/localhost:8808<\/li>\n<li><span style=\"line-height: 1.5;\">Rails is &#8220;full stack\u201d = \u201cdatabase to view\u201d<\/span><\/li>\n<li>Encourages convention over configuration<\/li>\n<li>Rails released July 2004 (due to the\u00a0popularity\u00a0of Ruby) &#8211; created by David Heinemeier<\/li>\n<li><strong><span class=\"code\">rails new &lt;APP-NAME&gt;<\/span><\/strong>\u00a0(app [models, views, controllers, assets, etc.], bin [scripts e.g. rails, rake, bundle], config, db, lib, log, public, test, tmp, vendor)\n<ul>\n<li>The <span class=\"code\">rails new<\/span> command will take a little while to run since it generates the beginning of your\u00a0Rails app\u00a0and\u00a0downloads all the basic gems needed to run a Rails app<\/li>\n<li>Rails assumes you will use Git, adding the .gitignore and .keep<\/li>\n<li>Rails creates a specific directory structure to be able to find everything programmatically (the magic of Rails &#8230; e.g. by default, Rails looks for templates in a file with the same name as the action it\u2019s handling)<\/li>\n<li>Use\u00a0<span class=\"code\">rake about<\/span> to examine your installation<\/li>\n<\/ul>\n<\/li>\n<li>Rails uses\u00a0SQLite 3 by default\n<ul>\n<li>Generally speaking, any site that gets fewer than 100K hits\/day should work fine with SQLite. The 100K hits\/day figure is a conservative estimate, not a hard upper bound. SQLite has been demonstrated to work with 10 times that amount of traffic.<\/li>\n<\/ul>\n<\/li>\n<li><strong><span class=\"code\">bin\/rails server<\/span>\u00a0(makes server run faster) or <span class=\"code\">rails server<\/span> to start Rails server (Ctrl+c to stop)<\/strong><\/li>\n<li><strong><span class=\"code\">bin\/rails console<\/span><\/strong> to start irb in Terminal\u00a0within your app<\/li>\n<li>Spring is an application pre-loader (speeds dev tasks)<\/li>\n<li>Bundler is itself\u00a0a gem<\/li>\n<li><span class=\"code\">bundle install<\/span>\u00a0ensures all required gems are installed<\/li>\n<li><span class=\"code\">bundle show<\/span><\/li>\n<li><span class=\"code\">bundle check<\/span><\/li>\n<li><span class=\"code\">bundle show &lt;gem&gt;<\/span><\/li>\n<li><span class=\"code\">bundle update &lt;gem&gt;<\/span><\/li>\n<li>localhost:3000<\/li>\n<li>No need to recompile! (except config for example)<\/li>\n<li>MVC architecture design pattern\n<ul>\n<li>User uses controller \u2026 manipulates model \u2026 updates view \u2026 which user sees<\/li>\n<\/ul>\n<\/li>\n<li>Rails is a request\/response framework\n<ul>\n<li>Requests are GET, POST, PUT, DELETE<\/li>\n<li>Responses are HTTP responses, have a status code (e.g. 200 OK, 302 Found, 404 Not Found)<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"code\">&lt;%= %&gt;<\/span> \u2026 executed AND output<\/li>\n<li><span style=\"line-height: 1.5;\"><span class=\"code\">&lt;% %&gt;<\/span> \u2026 executed<\/span><\/li>\n<li><strong>Rake<\/strong> is the build system for Ruby (you use rake to create the database)<\/li>\n<li>Controller actions render views<\/li>\n<li>If no explicit render is called, Rails automatically renders the template with the same name as the action<\/li>\n<li>Multiple extensions \u2026 index.html.erb \u2026 .html tells Rails the template generates HTML \u2026 .erb tells Rails to use the ERB (Embedded Ruby) templating system<\/li>\n<li><span class=\"code\">reload!<\/span> in the console to reload classes<\/li>\n<li>We are using the <strong>helper methods<\/strong> <span class=\"code\">number_with_delimiter<\/span> and <span class=\"code\">number_to_currency<\/span> to format the page count and price\n<ul>\n<li>Rails has many built in helper methods<\/li>\n<li>See the API docs or Rails Guides online<\/li>\n<\/ul>\n<\/li>\n<li>(e.g. books_path) is a generated function that comes from the name of the route in routes.rb<\/li>\n<li>You can see all the routes using <span class=\"code\">bin\/rake routes<\/span> or by navigating to http:\/\/localhost:3000\/rails\/info\/routes<\/li>\n<li>Class example &#8230; <span class=\"code\">class Mine &lt; Master<\/span> &#8230; the Mine Class inherits from the Master class<\/li>\n<li>Typical file name &#8230; hello.html<strong>.erb<\/strong> &#8230;\u00a0ERB is a filter that is installed as part of the Rails installation that takes an .erb file and outputs a transformed version\n<ul>\n<li>Content between &lt;%= and %&gt; is interpreted as Ruby code and executed<\/li>\n<\/ul>\n<\/li>\n<li>In development mode you don&#8217;t need to restart the server when you make changes<\/li>\n<li>Rails uses a convention to parse the URL into a target controller and an action within that controller\n<ul>\n<li><span class=\"code\">&lt;%= link_to(&#8220;DISPLAY_TEXT&#8221;), CONTROLLER_ACTION_path %&gt;<\/span><\/li>\n<\/ul>\n<\/li>\n<li>In views use <span class=\"code\">&lt;% &#8230; %&gt;<\/span> to inject Ruby and\u00a0<span class=\"code\">&lt;%= &#8230;\u00a0%&gt;<\/span> to inject Ruby value substitution<\/li>\n<li><strong>MVC<\/strong> &#8230; Model View Controller &#8230; Request comes into router which picks the appropriate controller, controller talks to model, model talks to database, model returns to controller, controller talks to view, view is rendered to the browser\n<ul>\n<li>Active Record is the ORM <strong>model<\/strong> layer supplied with Rails\n<ul>\n<li>ORM maps tables to classes, rows to objects, and columns to attributes<\/li>\n<\/ul>\n<\/li>\n<li>Action Pack is the <strong>view and controller<\/strong> layers in Rails\n<ul>\n<li>The controller is also home to a number of important ancillary services&#8230;\n<ul>\n<li>It is responsible for routing external requests to internal actions; handling people-friendly URLs extremely well<\/li>\n<li>It manages caching, which can give applications orders-of-magnitude performance boosts<\/li>\n<li>It manages helper modules, which extend the capabilities of the view templates without bulking up their code<\/li>\n<li>It manages sessions, giving users the impression of ongoing interaction with our applications<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>A &#8220;magic&#8221; command worth knowing is <span class=\"code\">rake db:migrate:redo<\/span> which will undo and reapply the last migration<\/li>\n<li>You can redo DB migrations with the following sequence&#8230;\n<pre>$ rake db:drop\r\n$ rake db:create\r\n$ rake db:migrate\r\n$ rake db:seed<\/pre>\n<\/li>\n<\/ul>\n<h2>Basic Command Flow<\/h2>\n<ul>\n<li><span class=\"code\">rails new &lt;app_name&gt;<\/span> to create the new project<\/li>\n<li><span class=\"code\">rails server<\/span> to start the local server<\/li>\n<li><span class=\"code\">rails generate controller NAME [action action] [options]<\/span> to create a new controller (NAME should be initial-capped and then camel-case since it becomes a Ruby class)\n<ul>\n<li>Modifies routes.rb, creates JS and CSS assets, creates controller, creates helper, creates views, and creates test controller<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Resources<\/h2>\n<ul>\n<li>Agile Web Development with Rails 4 (course book)<\/li>\n<li><a href=\"https:\/\/www.sqlite.org\/whentouse.html\" target=\"_blank\">SQLite.org<\/a><\/li>\n<li><a href=\"http:\/\/www.pragtob.info\/rails-beginner-cheatsheet\/\" target=\"_blank\">Rails Cheat Sheet<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>rails\u00a0-v to check version Rails takes MVC further: when you develop in Rails, you start with a working application, there\u2019s a place for each piece of code, and all the pieces of your application interact in a standard way Once you create an application with a specific version of Rails, it will continue to use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[152],"class_list":["post-3672","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-ruby"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3672"}],"version-history":[{"count":42,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3672\/revisions"}],"predecessor-version":[{"id":3951,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3672\/revisions\/3951"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}