<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jesse Sousa</title>
    <description>The latest articles on DEV Community by Jesse Sousa (@jessesousa).</description>
    <link>https://dev.to/jessesousa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F437996%2Fb187f0b9-d645-4e7a-867c-2824bb7ffe6b.jpg</url>
      <title>DEV Community: Jesse Sousa</title>
      <link>https://dev.to/jessesousa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jessesousa"/>
    <language>en</language>
    <item>
      <title>Learning Rails #2 - Migrations</title>
      <dc:creator>Jesse Sousa</dc:creator>
      <pubDate>Mon, 21 Mar 2022 00:14:45 +0000</pubDate>
      <link>https://dev.to/jessesousa/learning-rails-2-migrations-2952</link>
      <guid>https://dev.to/jessesousa/learning-rails-2-migrations-2952</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The role of these series, is to be used as a refresher and a place where beginners can discuss.&lt;br&gt;
If you're also learning (or intend to learn) Rails, I highly recommend that you follow the Getting Started section of the &lt;a href="https://guides.rubyonrails.org/"&gt;Rails Guides&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When creating a model, a migration file is also generated. You should see a file like &lt;code&gt;20130924230504_create_users.rb&lt;/code&gt; in the&lt;code&gt;db/migrate&lt;/code&gt; folder (this giant number is a timestamp). This is the migration file of your model and if you click it, you see that inside that are all attributes that you made and its types.&lt;br&gt;
After creating a model you have to run the migration, in order to update the database, creating a new table for the model.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is a Migration?
&lt;/h2&gt;

&lt;p&gt;A Migration basically is a script that will have all the code needed to change  your database. Basically every change you make to the database should be through a migration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; Let's say you created a model for users, and ran its migrations, then you realize you forgot the email attribute, you can solve this problem by creating a new migration file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rails generate migration AddEmailToUsers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in the migration file, you can add any changes, in our case we need to add an email attribute, so we'll do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# In the migration file&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AddEmailToUsers&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;7.0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;add_column&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:string&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rails db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all migrations will run, hence the database will be updated.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use Migrate and Rollback
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rails db:migrate&lt;/code&gt;&lt;br&gt;
As we've seen before, this will run all the migrations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rails db:rollback&lt;/code&gt;&lt;br&gt;
It will run the inverse command for everything a migration did, sometimes you'll need to write it yourself check the &lt;a href="https://guides.rubyonrails.org/active_record_migrations.html#using-the-up-down-methods"&gt;documentation&lt;/a&gt; for more information.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By default, the commands above will run all the migrations you have, using the timestamp to track the order of when which script should run. To run only the migrations you want, use the &lt;code&gt;STEP&lt;/code&gt; parameter. Ex.: &lt;code&gt;rails db:migrate STEP=2&lt;/code&gt;, this will run only two migrations files.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning Rails #1 - MVC</title>
      <dc:creator>Jesse Sousa</dc:creator>
      <pubDate>Mon, 14 Mar 2022 23:27:16 +0000</pubDate>
      <link>https://dev.to/jessesousa/learning-rails-1-mvc-4e6j</link>
      <guid>https://dev.to/jessesousa/learning-rails-1-mvc-4e6j</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is the first post of a series called Learning Rails, where I explain everything I learn during my journey to mastering Rails. The role of these series, is to work as a refresher and a place where beginners can discuss.&lt;br&gt;
If you're also learning (or intend to learn) Rails, I highly recommend that you follow the &lt;a href="https://guides.rubyonrails.org/getting_started.html"&gt;Getting Started with Rails Guide&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Before we start, here are some things to note about Rails:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;"Convention over configuration"&lt;/strong&gt; - You'll realize that Rails does a lot of things &lt;em&gt;implicitly&lt;/em&gt; for us, it happens because Rails defaults to a set of conventions. So we can spend more time coding rather than configuring.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No need for importing files&lt;/strong&gt; - Rails utilizes autoload, meaning that it loads all necessary files during execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Naming matters&lt;/strong&gt; - All my examples follow the naming conventions, but I won't be focusing on explaining them here, the Guides has everything you need to know.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is MVC?
&lt;/h2&gt;

&lt;p&gt;MVC stands for Model View Controller and is the architecture used by the framework. In order to learn Rails, you have to understand MVC. I'll give a brief explanation of what each component does (and how to create them), but remember to always read the documentation if you need a deeper understanding.&lt;/p&gt;

&lt;p&gt;When creating a project with rails in the command line (&lt;code&gt;$ rails new &amp;lt;project-name&amp;gt;&lt;/code&gt;), a bunch of folders will be generated. For now, we'll be focusing on the &lt;code&gt;app/&lt;/code&gt; folder. You can see an explanation for each directory, on the &lt;a href="https://guides.rubyonrails.org/getting_started.html#creating-the-blog-application"&gt;Rails Guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model
&lt;/h2&gt;

&lt;p&gt;The Model class is responsible for our data, it represents an object of our application and it also connects to the database by inheriting the ActiveRecord class. Here we can validate, query, add, update and delete our data from the database.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  The Active Record class
&lt;/h3&gt;

&lt;p&gt;The Active Record class is responsible for handling the connection and queries of our database. It is the magic behind writing ruby code for manipulating our database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Creating a Model&lt;/strong&gt;&lt;br&gt;
On the terminal, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;rails generate model Article title:string body:text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we're saying the name of our Model and its attributes joined by &lt;code&gt;:&lt;/code&gt; with the attribute type.&lt;/p&gt;

&lt;h2&gt;
  
  
  View
&lt;/h2&gt;

&lt;p&gt;This is the most straightforward one, it is the visual part of our application, so it includes all of our files that renders the visual part of our application. &lt;/p&gt;

&lt;p&gt;On rails, we generally use ERB(Embedded Ruby) for our views, which is a template engine. This allows us for rendering static pages with all information we want in a easier way, for example, rather than creating manually a list with all articles, we can use Ruby's &lt;code&gt;#each&lt;/code&gt; method to do that, soon you'll better understand how this works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To create a View&lt;/strong&gt;, simply go to the &lt;code&gt;app/views/&amp;lt;Controller Name&amp;gt;&lt;/code&gt; and create a new &lt;code&gt;.html.erb&lt;/code&gt; file. The filename should match with a controller action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;Here is where we bind all of this together, the controller class contains various methods (usually called actions in the context of Rails) responsible for rendering views and handling our models. It acts as the middleman, since it handles requests and responses in our application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Controller&lt;/strong&gt;&lt;br&gt;
On the terminal, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;rails generate controller Articles
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember to always create it with a plural name, following the naming convention.&lt;/p&gt;

&lt;h2&gt;
  
  
  Routes
&lt;/h2&gt;

&lt;p&gt;The routes file is where we map all our requests and responses, to create a new route we define the path, HTTP verb and action for our route. The action is actually a method for a determined controller, as I've mentioned before.&lt;/p&gt;

&lt;p&gt;Here is the syntax for creating a new route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# In `config/routes.rb`&lt;/span&gt;
&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# Every time a GET request is made to the `/articles` path the&lt;/span&gt;
  &lt;span class="c1"&gt;# `#index` method from the articles controller will be called&lt;/span&gt;
  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/articles'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'articles#index'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  RESTful routes
&lt;/h2&gt;

&lt;p&gt;For our routes we'll be using the REST(Representational State Transfer) architecture, which has to do on how we implement our routes. REST consists of 7 routes needed to create a CRUD for our application. &lt;br&gt;
To better illustrate these routes, let's imagine we have a blog and we want routes to create/read/update/delete(CRUD) our articles. By using the REST architecture to create our routes, it will result in the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# In `config/routes.rb`&lt;/span&gt;
&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# Get all articles&lt;/span&gt;
  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/articles'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'articles#index'&lt;/span&gt;

  &lt;span class="c1"&gt;# Get one single article&lt;/span&gt;
  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/articles/:id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'articles#show'&lt;/span&gt;

  &lt;span class="c1"&gt;# Get form view for creating a new article&lt;/span&gt;
  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/articles/new'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'articles#new'&lt;/span&gt;

  &lt;span class="c1"&gt;# Create new article&lt;/span&gt;
  &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s1"&gt;'/articles'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'articles#create'&lt;/span&gt;

  &lt;span class="c1"&gt;# Get form view for editing an article&lt;/span&gt;
  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/articles/:id/edit'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'articles#edit'&lt;/span&gt;

  &lt;span class="c1"&gt;# Update our article&lt;/span&gt;
  &lt;span class="n"&gt;put&lt;/span&gt; &lt;span class="s1"&gt;'/articles/:id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'articles#update'&lt;/span&gt;

  &lt;span class="c1"&gt;# Delete an article&lt;/span&gt;
  &lt;span class="n"&gt;delete&lt;/span&gt; &lt;span class="s1"&gt;'/articles/:id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'articles#delete'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how all the actions follow a naming convention, these are not random, it is actually the standard naming for such routes. We can replace the code above by using the keyword &lt;code&gt;resources&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# In `config/routes.rb`&lt;/span&gt;
&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# This creates all 7 RESTful routes&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:articles&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this, ruby will use the naming convention, I've mentioned above, to implicitly create all the 7 RESTful routes and its action calls.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Rails Console
&lt;/h2&gt;

&lt;p&gt;The Rails Console is a way of accessing our application via the Command Line, here we can create components and test them.&lt;/p&gt;

&lt;p&gt;In the command line, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;rails console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will open an IRB(Interactive Ruby) instance with Rails and the application code loaded. So we can create a new article by using the Article Model class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Here I'm assuming there is an article model that contains a&lt;/span&gt;
&lt;span class="c1"&gt;# `title:string` and `body:text`&lt;/span&gt;

&lt;span class="c1"&gt;# Inside Rails console, run:&lt;/span&gt;

&lt;span class="c1"&gt;# This creates a new Article object&lt;/span&gt;
&lt;span class="n"&gt;article&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'New Article'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'This is a new article.'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# This save our object to the database&lt;/span&gt;
&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;

&lt;span class="c1"&gt;# Quit the Rails console&lt;/span&gt;
&lt;span class="n"&gt;quit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Now that we have an article, we can render it on our page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Requests in our Controller
&lt;/h2&gt;

&lt;p&gt;To send a response, we'll need to map the request to the right action. If the &lt;code&gt;resources&lt;/code&gt; keyword was used, the route &lt;code&gt;GET /articles&lt;/code&gt; will call the &lt;code&gt;#index&lt;/code&gt; action.&lt;/p&gt;

&lt;p&gt;We want the response to be a page rendering all the articles our blog currently has. To do that we can create the &lt;code&gt;#index&lt;/code&gt; action in our controller, get all our articles and store it into an instance variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Inside `app/articles_controller.rb`&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TasksController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="vi"&gt;@articles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's one thing to notice, Rails implicitly calls a render (and passes our instances variables) for a view with the same name of our action, so a call for &lt;code&gt;#index&lt;/code&gt; in the articles controller will render the file &lt;code&gt;app/views/articles/index.html.erb&lt;/code&gt;, remember "Convention over Configuration" now you're seeing this in action. So let's create that file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;%# In `app/views/articles/index.html.erb` %&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Index&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;%# Here we can use: `@articles` %&amp;gt;&lt;/span&gt; 
&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="vi"&gt;@articles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;body&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it. That's pretty much how the MVC architecture works in Rails. &lt;/p&gt;

&lt;p&gt;Remember these series should only be used as a refresher, the way of really learning Rails is by reading the Guides. &lt;/p&gt;

&lt;p&gt;Don't forget to like, save and ask questions (I'll be answering everything I can, just remember I'm not an expert YET). Also, if you have experience with Rails, feel free to point out topics that I might be missing and suggestions you might have.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>How to deploy your React App with Github Pages</title>
      <dc:creator>Jesse Sousa</dc:creator>
      <pubDate>Sat, 22 Aug 2020 21:26:50 +0000</pubDate>
      <link>https://dev.to/jessesousa/how-to-deploy-your-react-app-with-github-pages-15b9</link>
      <guid>https://dev.to/jessesousa/how-to-deploy-your-react-app-with-github-pages-15b9</guid>
      <description>&lt;p&gt;Github Pages is a nice way to host your website, it's completely free and very easy to deploy, and you can use it with any framework not only React. I didn't know that when I was a beginner and I thought it would be nice to share it.&lt;/p&gt;

&lt;p&gt;Before starting, make sure your app is hosted in a Github Repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Install gh-pages
&lt;/h2&gt;

&lt;p&gt;In your terminal type, &lt;code&gt;npm install gh-pages --save-dev&lt;/code&gt; or if you're using yarn &lt;code&gt;yarn add gh-pages --dev&lt;/code&gt;. The gh-pages dependency is what you're going to be using to deploy your app.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Add the scripts and the homepage URL
&lt;/h2&gt;

&lt;p&gt;In your &lt;code&gt;package.json&lt;/code&gt; file add:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"homepage": "https://[your github username].github.io/[your project name]"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Make sure that your project name is the same used in your GitHub repository.&lt;br&gt;
Then add to scripts:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"predeploy": "npm build",&lt;br&gt;
"deploy": "gh-pages -d build"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Time to deploy
&lt;/h2&gt;

&lt;p&gt;Finally, on your terminal type, &lt;code&gt;npm run deploy&lt;/code&gt; and that's it, as I said, very simple. It may take a couple of minutes before your app gets hosted but if everything works fine you should be able to see your app with the URL that you used for the homepage. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>react</category>
    </item>
    <item>
      <title>Create your own Markdown Previewer with React</title>
      <dc:creator>Jesse Sousa</dc:creator>
      <pubDate>Sat, 15 Aug 2020 20:47:40 +0000</pubDate>
      <link>https://dev.to/jessesousa/create-your-own-markdown-previewer-with-react-292d</link>
      <guid>https://dev.to/jessesousa/create-your-own-markdown-previewer-with-react-292d</guid>
      <description>&lt;p&gt;When I was starting to learn how to code I never thought I could do my own Markdown Previewer. I thought it was too advanced and that it would be impossible to do it on my own. But later I learned that it is quite simple, I mean it is not simple to parse Markdown code, but we can use packages to do all the hard work for us. And only care about the styling.&lt;/p&gt;

&lt;p&gt;And that's what we're gonna be building today, in this tutorial, I'll be using React, but it can be easily done with vanilla js.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Create your React project
&lt;/h2&gt;

&lt;p&gt;On your terminal type the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app markdown-previewer
npm install --save react-markdown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first command will generate our app, and the second command will add react-markdown to our dependencies. The react-markdown is the dependency that will parse markdown to jsx for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Planning and Creating our first components
&lt;/h2&gt;

&lt;p&gt;Our App is gonna be very simple, the UI will be divided into 2 panels. The left one being the input, and the right one being the output.&lt;/p&gt;

&lt;p&gt;So let's create them. On your src folder create both components, with MarkdownInput being a &lt;strong&gt;&lt;em&gt;textarea&lt;/em&gt;&lt;/strong&gt; and the MarkdownOutput a &lt;strong&gt;&lt;em&gt;div&lt;/em&gt;&lt;/strong&gt; tag. Inside App.js let's add markdownInput to our state, then assign it with an empty string, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;markdownInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time a user types on the form we want to update the markdown input value, to do so let's create a function that sets the state for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;markdownInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's pass it down as a prop to the MarkdownInput component and also pass markdownInput to our MarkdownOutput component. On App.js inside the render function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MarkdownInput&lt;/span&gt; &lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MarkdownOutput&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;markdownInput&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On MarkdownInput.js, add the handleChange function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And inside the render function, add a textarea element and add a onChange event listener.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;textarea&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/textarea&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On MarkdownOutput.js, import the ReactMarkdown component from react markdown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactMarkdown&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-markdown&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And inside the render function, add a div element wrapping the ReactMarkdown component. To our ReactMarkdown component render our markdown we need to pass our markdown to the source prop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ReactMarkdown&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it! Now, all we need to do is to add our styles. Here's the &lt;a href="https://github.com/JesseSousa/markdown-viewer-react"&gt;link&lt;/a&gt; to my repository in case you want the same styles that I'm using. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
