<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gerry Mercado on Medium]]></title>
        <description><![CDATA[Stories by Gerry Mercado on Medium]]></description>
        <link>https://medium.com/@gerrymercado?source=rss-ab985ca2ebdf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DiRey8yc2W9eAoy9R3tuHA.jpeg</url>
            <title>Stories by Gerry Mercado on Medium</title>
            <link>https://medium.com/@gerrymercado?source=rss-ab985ca2ebdf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 05:42:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gerrymercado/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Delete Requests]]></title>
            <link>https://medium.com/@gerrymercado/a-delete-request-7f3d541253a9?source=rss-ab985ca2ebdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f3d541253a9</guid>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Gerry Mercado]]></dc:creator>
            <pubDate>Mon, 25 Jun 2018 06:23:46 GMT</pubDate>
            <atom:updated>2018-06-25T16:37:11.161Z</atom:updated>
            <content:encoded><![CDATA[<h4>You Don’t Have to Remove it from the Database!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*TRwLFPz1nnnGwZTKNdG2Mg.png" /></figure><p>Up until now, inside of our products’ application, we’ve learned how to remove a product from the database using a delete request that starts in our routes file, makes its way to the controller destroy action and renders a flash message to the user that the product was successfully deleted as they are redirected to the products’ index page.</p><p>But as we developed our application and added the feature of a “cart,” where users can place products in a cart to purchase them, we needed to have the ability for them to remove a product from the cart. It wouldn’t be helpful if they removed it from the cart and it also got removed from the database.</p><p>Thankfully, we are able to solve this problem inside of our destroy method by calling on the update method for our instance variable, @carted_product, in this case. Looks like this inside of our back-end application in the carted_products_controller:</p><pre>def destroy<br>    <a href="http://twitter.com/carted_product">@carted_product</a> = CartedProduct.find_by(id: params[:id])<br>    <a href="http://twitter.com/carted_product">@carted_product</a>.update(status: &quot;removed&quot;)<br>    render json: {message: &quot;Carted product successfully removed&quot;}<br>  end</pre><p>In the frontend, that’s where things got tricky for me. First, I made sure I understood this code we worked on together in class, inside of the client carted_products_controller.</p><pre>def destroy<br>    carted_product_id = params[:id]<br>    response = Unirest.delete(&quot;<a href="http://localhost:3000/api/carted_products/#{carted_product_id">http://localhost:3000/api/carted_products/#{carted_product_id</a>}&quot;)<br>    flash[:success] = &quot;Product successfully removed from cart!&quot;<br>    redirect_to &quot;/client/carted_products/&quot;<br>  end</pre><p>Our Unirest.delete request goes off to the back end controller inside of the destroy method in order to update the status to “removed” in our database and show the message that the product was removed for the user. However, we also need to look into our carted_products index.html.erb file because thats where we created a view for our shopping cart with all the data about the product we want to render for each carted_product.</p><pre>&lt;h2&gt;Your Shopping Cart&lt;/h2&gt;</pre><pre>&lt;% <a href="http://twitter.com/carted_products">@carted_products</a>.each do |carted_product| %&gt;</pre><pre>&lt;p&gt;Id: &lt;%= carted_product[&quot;id&quot;] %&gt;&lt;/p&gt;  <br>  &lt;p&gt;Product Name: &lt;%= carted_product[&quot;product&quot;][&quot;name&quot;]%&gt;&lt;/p&gt;<br>   &lt;p&gt;Product Price: &lt;%= carted_product[&quot;product&quot;][&quot;formatted&quot;][&quot;price&quot;]%&gt;&lt;/p&gt;<br>  &lt;p&gt;Quantity: &lt;%= carted_product[&quot;quantity&quot;] %&gt;&lt;/p&gt;<br>  &lt;a href=&quot;/client/carted_products/&quot;data-method=&quot;delete&quot;&gt;&lt;button&gt;Remove&lt;/button&gt;&lt;/a&gt;&lt;br&gt;<br>&lt;% end %&gt;<br>&lt;br&gt;<br>&lt;a href=&quot;/client/orders&quot; data-method=&quot;post&quot; &gt;&lt;button&gt;Order!&lt;/button&gt;&lt;/a&gt;</pre><p>Then this happened:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*KD43zy-hbtSEayEUW9Ba8Q.png" /></figure><p>I saw that my backend server log rendered a 200 OK status but it took me a while to figure out why I had this error in my front end. But then I had an aha moment. We need to be specific with the product id that gets passed in the route for Rails to know what product is getting removed. We pass the id of the carted product like this using embedded ruby syntax.</p><pre>&lt;a href=&quot;/client/carted_products/<strong>&lt;%= carted_product[&#39;id&#39;] %&gt;</strong>&quot; data-method=&quot;delete&quot;&gt;&lt;button&gt;Remove&lt;/button&gt;&lt;/a&gt;&lt;br&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*Ug5_zuZYScWRwTEfZZLljQ.png" /></figure><p>A final trick I’d like to share is that when I hovered my mouse over the remove button I created, I noticed that it showed the id of the product I was about the remove in the url. But when I was getting the error, I didn’t see the product id in the url. I only saw the “/client/carted_products/” for which no route matched my delete request.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f3d541253a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My First Lesson in SQL]]></title>
            <link>https://medium.com/@gerrymercado/my-first-lesson-in-sql-7359e112fb05?source=rss-ab985ca2ebdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/7359e112fb05</guid>
            <category><![CDATA[beginner-coding]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[bootcamp]]></category>
            <dc:creator><![CDATA[Gerry Mercado]]></dc:creator>
            <pubDate>Sat, 09 Jun 2018 06:09:03 GMT</pubDate>
            <atom:updated>2018-06-09T07:19:38.613Z</atom:updated>
            <content:encoded><![CDATA[<h4>Binding Statements and Variables</h4><p>Recently in class, we practiced creating SQL statements inside of our controller files in a contacts application we created in Rails.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*OtfUYjuLsYL0zMtRkXV30Q.png" /></figure><p>At first, the syntax for creating SQL statements inside of Rails seemed daunting. But with practice in class, I’m starting to feel more comfortable. One of the features we were asked to implement was to add an option to search for any contact that has a specific name. I was able to create SQL code to search for first_name in the backend. It looked like this:</p><pre>def index<br>    <a href="http://twitter.com/contacts">@contacts</a> = Contact.all<br>    if params[:search]<br>      <a href="http://twitter.com/contacts"><strong>@contacts</strong></a><strong> = Contact.where(&quot;first_name LIKE ?&quot;,              &quot;%#{params[:search]}%&quot;)</strong><br>    end<br>    render &quot;index.json.jbuilder&quot;<br>end</pre><p>Afterwards, we had to expand our search to make all other attributes searchable in our Contact model. I started by adding last_name as a searchable attribute. Here’s how I originally typed my binding statement:</p><pre>def index<br>    <a href="http://twitter.com/contacts">@contacts</a> = Contact.all<br>    if params[:search]<br>      <a href="http://twitter.com/contacts">@contacts</a> = <strong>Contact.where(&quot;first_name LIKE ? OR last_name LIKE ?&quot;, &quot;%#{params[:search]}%&quot;)</strong><br>    end<br>    render &quot;index.json.jbuilder&quot;<br>end</pre><p>I received an error that I didn’t understand at first when searching for a last name in the database. My log inside my terminal for my backend application said that I was passing the wrong number of bind variables (1 for 2). Ruby is very specific with its errors but I needed to first understand what a binding variable was.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*BvaDgOAV5iPAKU3FCtmhfg.png" /></figure><p>After trying different variations, I searched on Stack Overflow and discovered that my binding variable was the:searchkey. In this application, we passed the key :search as the variable inside of params. Therefore, the solution was to repeat our variable inside of our string interpolated params like this:</p><pre>def index<br>    <a href="http://twitter.com/contacts">@contacts</a> = Contact.all</pre><pre>    if params[:search]<br>      <a href="http://twitter.com/contacts">@contacts</a> = Contact.where(&quot;first_name LIKE ? OR last_name LIKE ?&quot;, <strong>&quot;%#{params[:search]}%&quot;, &quot;%#{params[:search]}&quot;</strong>)<br>    end</pre><p>This was a good lesson to learn early on as I begin to implement SQL into my Rails’ applications. Check your logs in your terminal as you run your requests! Details here are very specific and it’s a great starting point to resolving your errors.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7359e112fb05" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All About The Flow]]></title>
            <link>https://medium.com/@gerrymercado/all-about-the-flow-d76c82aea7f6?source=rss-ab985ca2ebdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/d76c82aea7f6</guid>
            <category><![CDATA[codingbootcamp]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[api-development]]></category>
            <dc:creator><![CDATA[Gerry Mercado]]></dc:creator>
            <pubDate>Mon, 28 May 2018 19:48:19 GMT</pubDate>
            <atom:updated>2018-05-28T19:55:28.123Z</atom:updated>
            <content:encoded><![CDATA[<h4>Three Main Steps To Creating A Rails Application</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*82FIJaN5CGoWlAPt7lbI2Q.jpeg" /></figure><p>After many attempts and addressing errors as they came up, I created a simple Rails application to store contacts that would serve as an API in JSON format. There are three key steps I learned to navigate through when creating my app:</p><ol><li>API Route</li><li>API Controller</li><li>API View</li></ol><p>I challenged myself to not look at the examples created in class as I was going through the flow. It was hard but worth it. I kept working through the error messages and learned to take it one step at a time. One cool thing about Rails is that it is very specific with its error messages.</p><p>After creating my app in the terminal inside of my desired directory, I opened it in my Sublime text editor and accessed my routes file. There, I established a namespace called “api” to help my user understand that I’m creating an API application to retrieve data from. Looked like this:</p><pre>namespace :api do</pre><pre>end</pre><p>Then I made a get request where I defined a url and directed my application to the controller file that held my methods. I had to use the terminal to establish my controller. This is what it looks like:</p><pre>rails generate controller namespace/name_of_your_controller</pre><p>I also used my terminal to create a model if my app required it to store data about items or contacts, for example. Finally, inside of my controller file, I had to direct the application to render a view in the browser using the keyword render and leveraging the jbuilder gem by including it in quotes. For my contacts app, it looked something like this:</p><pre>render “contacts_view.json.jbuilder&quot;</pre><p>In summary, I&#39;m glad I got a better understanding of the flow between API Route &gt; API Controller &gt; API View when creating a Rails application. I look forward to taking it the next level as I add more urls, methods, and views to my apps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d76c82aea7f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>