{"id":5254,"date":"2023-10-21T09:59:50","date_gmt":"2023-10-21T09:59:50","guid":{"rendered":"https:\/\/palplanner.com\/schools\/?p=5254"},"modified":"2023-10-23T11:47:04","modified_gmt":"2023-10-23T11:47:04","slug":"getting-started-with-ruby-on-rails-hello-world-with-rails","status":"publish","type":"post","link":"https:\/\/palplanner.com\/schools\/getting-started-with-ruby-on-rails-hello-world-with-rails\/","title":{"rendered":"Getting Started with Ruby on Rails: Hello World with Rails"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ruby on Rails, often referred to simply as Rails, is a powerful and popular web application framework known for its elegant and developer-friendly design. Developed by David Heinemeier Hansson in 2005, Rails has gained a devoted following due to its simplicity and convention over configuration philosophy. If you&#8217;re new to Rails and want to dip your toes into web development, this &#8220;Hello World&#8221; tutorial will guide you through the process of creating a basic Rails application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving into the Rails world, make sure you have the following prerequisites:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Ruby<\/strong>: Rails is built on the Ruby programming language. You&#8217;ll need to have Ruby installed on your system. You can check your Ruby version by running <code>ruby -v<\/code> in your terminal. If it&#8217;s not installed, follow the instructions for your specific operating system from <a href=\"https:\/\/www.ruby-lang.org\/en\/documentation\/installation\/\">ruby-lang.org<\/a>.<\/li>\n\n\n\n<li><strong>RubyGems<\/strong>: RubyGems is the package manager for Ruby. It&#8217;s usually bundled with Ruby, but you can check if it&#8217;s available by running <code>gem -v<\/code>. If it&#8217;s not installed, you can install it by following the instructions on the <a href=\"https:\/\/rubygems.org\/pages\/download\">RubyGems website<\/a>.<\/li>\n\n\n\n<li><strong>SQLite (optional)<\/strong>: Rails uses SQLite as the default database. It should be installed by default on most systems. However, if you plan to use a different database like MySQL or PostgreSQL, make sure it&#8217;s installed and correctly configured.<\/li>\n\n\n\n<li><strong>A Text Editor<\/strong>: You&#8217;ll need a code editor to write your Rails application. Some popular choices include Visual Studio Code, Sublime Text, and Atom.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Rails<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you&#8217;ve got Ruby and RubyGems installed, it&#8217;s time to install Ruby on Rails. Open your terminal and run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gem install rails<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command will download and install the latest version of Rails. After the installation is complete, you can verify it by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rails -v<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see the version number displayed in your terminal, confirming a successful installation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Your First Rails Application<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With Rails installed, you&#8217;re ready to create your first Rails application. Rails provides a powerful tool called <code>rails new<\/code> that generates the basic structure for a new project. Let&#8217;s create a simple &#8220;Hello World&#8221; application:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open your terminal and navigate to the directory where you want to create your Rails application. Use the <code>cd<\/code> command to change directories.<\/li>\n\n\n\n<li>Run the following command to create a new Rails application called &#8220;HelloWorldApp&#8221;:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>rails new HelloWorldApp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command will generate the necessary files and directories for your Rails application.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>After the generation process is complete, navigate to your application&#8217;s directory:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>cd HelloWorldApp<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Writing Your First Controller and View<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In a Rails application, the logic is divided into controllers and views. Controllers handle requests and send data to views for presentation. Let&#8217;s create a simple &#8220;Hello, World!&#8221; message.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Generate a new controller named &#8220;Welcome&#8221; with an action (a method) called &#8220;index&#8221; using this command:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>rails generate controller Welcome index<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command creates a new controller file and an associated view.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>Open the <code>app\/controllers\/welcome_controller.rb<\/code> file in your text editor. You&#8217;ll see a <code>WelcomeController<\/code> class with an <code>index<\/code> action. Modify it to look like this:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>class WelcomeController &lt; ApplicationController\n  def index\n    @message = \"Hello, World!\"\n  end\nend<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, we&#8217;ve defined a variable <code>@message<\/code> with the message we want to display.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>Now, navigate to the <code>app\/views\/welcome<\/code> directory and open the <code>index.html.erb<\/code> file in your text editor. Replace its content with the following code:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1&gt;&lt;%= @message %&gt;&lt;\/h1&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code is an ERB (Embedded Ruby) template that will display the message stored in the <code>@message<\/code> variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring Routes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Rails, routes determine how incoming requests are routed to the appropriate controller actions. Let&#8217;s configure a route to our <code>WelcomeController#index<\/code> action.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the <code>config\/routes.rb<\/code> file in your text editor. Add the following line to define a root route:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>root 'welcome#index'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This tells Rails to route the root URL to the <code>WelcomeController<\/code>&#8216;s <code>index<\/code> action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Starting the Server<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you&#8217;ve created your &#8220;Hello World&#8221; application, it&#8217;s time to start the development server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rails server<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll see some output in your terminal, including a message indicating that the server is running. By default, your Rails application will be accessible at <code>http:\/\/localhost:3000<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open your web browser and navigate to that URL, and you should see the &#8220;Hello, World!&#8221; message displayed on the page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations! You&#8217;ve just built your first Ruby on Rails application. You&#8217;ve learned how to create a basic controller, view, and route. This is just the beginning of your journey into the world of Rails, a powerful and flexible framework for building web applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From here, you can explore more advanced features, such as creating models and working with databases, implementing user authentication, and building interactive web applications with the rich ecosystem of Ruby on Rails. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby on Rails, often referred to simply as Rails, is a powerful and popular web application framework known for its elegant and developer-friendly design. Developed by David Heinemeier Hansson in 2005, Rails has gained a devoted following due to its simplicity and convention over configuration philosophy. If you&#8217;re new to Rails and want to dip [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[52],"class_list":["post-5254","post","type-post","status-publish","format-standard","hentry","category-programming","category-uncategorized","tag-ror"],"_links":{"self":[{"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/posts\/5254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/comments?post=5254"}],"version-history":[{"count":1,"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/posts\/5254\/revisions"}],"predecessor-version":[{"id":5255,"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/posts\/5254\/revisions\/5255"}],"wp:attachment":[{"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/media?parent=5254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/categories?post=5254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/palplanner.com\/schools\/wp-json\/wp\/v2\/tags?post=5254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}