<?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: Ayushman Parasar</title>
    <description>The latest articles on DEV Community by Ayushman Parasar (@ayushmann).</description>
    <link>https://dev.to/ayushmann</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%2F287854%2Fa1470b4e-bbe5-4510-aef5-a6444792599a.jpeg</url>
      <title>DEV Community: Ayushman Parasar</title>
      <link>https://dev.to/ayushmann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ayushmann"/>
    <language>en</language>
    <item>
      <title>How to use json-jbuilder in Rails</title>
      <dc:creator>Ayushman Parasar</dc:creator>
      <pubDate>Mon, 24 Aug 2020 17:24:25 +0000</pubDate>
      <link>https://dev.to/ayushmann/how-to-use-json-jbuilder-in-rails-49ef</link>
      <guid>https://dev.to/ayushmann/how-to-use-json-jbuilder-in-rails-49ef</guid>
      <description>&lt;p&gt;This post is just a mere attempt to log-in what I have learnt about json-jbuider and is not a tutorial but I know this will help those who have just began to learn Rails.&lt;/p&gt;

&lt;p&gt;Okay then lets start!!&lt;/p&gt;

&lt;p&gt;First of all Jbuilder is a template for rendering json responses. Rails comes preloaded with jbuilder gem which helps to create JSON structures. Lets see its use-case scenerio:&lt;/p&gt;

&lt;p&gt;For example we are making a blog app in which we have a lot of articles and users who create those articles. So we have two models a User model and an Article model. A User model has many articles and an Article model belongs to User.In Article model we have a reference to the user model through user-id(basically author of the article). Now suppose we have a case where we need to show the list of articles with few details of each article including author information. In the articles controller we have the following action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def index
  @articles = Article.all
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;b&gt;@articles&lt;/b&gt; is an array containing all the articles and each article has the info corresponding to that particular row of the article table. We also need the author information but we have only the author id in the article table. This is the case where jbuider comes into flow.&lt;/p&gt;

&lt;p&gt;By default rails looks for a template of the requested format. For rendering response to JSON query we will need a template. We make a template called index.json.jbuilder in the views/api/v1/aricles directory. This file contains the logic for populating the nested attributes of the article model(eg: user). Here we have namespaced the  directory because I think its better to have a clear seperation of our APIs.&lt;br&gt;
In the index.json.jbuider add the following lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;json.articles @articles do |article|
  json.partial "article", obj: article
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above lines, we are defining a key in json called articles and we are taking the array &lt;b&gt;@articles&lt;/b&gt; that we made in the articles controller and we are iterating over each article of the array and rendering a partial with an object "obj" containing the article details. Partial rendering is one of the plus points of using jbuilder.&lt;/p&gt;

&lt;p&gt;As of now we dont have a partial so lets make one and name it "_article.json.jbuilder" in the same directory i.e views/api/v1/articles. Now lets add the following lines in the partial:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;json.id obj.id
json.title obj.title
json.author do 
  json.user_id obj.user.id
  json.name obj.user.name
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is happening above is that we are structuring a json file where json.id , json.title, json.author are the keys . Here we are able to get user details other than the id.&lt;br&gt;
There is one more way of doing this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;json.(obj, :id, :title) 
json.(obj.user, :id, :name) 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But I find the first one more useful.&lt;/p&gt;

&lt;p&gt;I hope this article was useful and if there was anything I explained incorrectly or if there was a different apprach or if I missed anything do let me know. Thanks!!!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What to choose VAR, LET or CONST?</title>
      <dc:creator>Ayushman Parasar</dc:creator>
      <pubDate>Thu, 19 Dec 2019 14:43:01 +0000</pubDate>
      <link>https://dev.to/ayushmann/what-to-choose-var-let-or-const-k9c</link>
      <guid>https://dev.to/ayushmann/what-to-choose-var-let-or-const-k9c</guid>
      <description>&lt;p&gt;When we write a program in javascript we use a lot of variables. These variables play an important role as they are name storage of data. So whenever we are declaring a variable we need to do it correctly so as to write error-free code. We can declare the variables using &lt;em&gt;var&lt;/em&gt; , &lt;em&gt;const&lt;/em&gt; or &lt;em&gt;let&lt;/em&gt; .The behavior of let and const is somewhat similar whereas for &lt;em&gt;var&lt;/em&gt; the scenario changes. Earlier there was no &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt;  , but only &lt;em&gt;var&lt;/em&gt; . With ES6 both &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; came into action which was a relief for the developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So let us SEE HOW THEY ARE DIFFERENT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In terms of assigning:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 4;
let a = 5;//wrong
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once I have done this let a = 4;&lt;br&gt;
I cannot do let a = 5 within the same scope now the question is why?&lt;/p&gt;

&lt;p&gt;When we are using &lt;em&gt;let&lt;/em&gt; the following things happen in the above code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;it creates a space for ‘a’,&lt;/li&gt;
&lt;li&gt;it assigns the value of 4 to a,
Now when we try let a = 5 there is a syntax error that the identifier &lt;em&gt;a&lt;/em&gt; has already been declared, this is because it tries to create a second space for the same variable &lt;em&gt;a&lt;/em&gt; which is not possible with the use of &lt;em&gt;let&lt;/em&gt;.
&lt;em&gt;const&lt;/em&gt; behaves in a similar way but the  difference is that we need to assign a value at the time of declaration of the variable. That being said it should be &lt;em&gt;const a = 5&lt;/em&gt; and not &lt;em&gt;const a;&lt;/em&gt;.The value of a variable declared with &lt;em&gt;const&lt;/em&gt; cannot be updated or re-declared that is it remains the same within its scope
Now let us come to &lt;em&gt;var&lt;/em&gt;, when we write &lt;em&gt;var a = 5&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;It allocates space for &lt;em&gt;a&lt;/em&gt; and assigns it to the value of undefined.&lt;/li&gt;
&lt;li&gt;It assigns the value 5 to &lt;em&gt;a&lt;/em&gt;
If we reassign &lt;em&gt;var a = 6&lt;/em&gt; then there would be no errors this time. This is because at the time when javascript was made it was never made to be at the scale where it is now, so many things were not planned and as such is the case of &lt;em&gt;var&lt;/em&gt; .
There is one more thing I got to know is that when we declare variables using &lt;em&gt;var&lt;/em&gt; outside of function it becomes a global variable and becomes accessible to the window object which can cause issues in the program but this is not the case with let as it does not attach the variable with the window object.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;In terms of scope:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, let us understand what is meant by scope, we can take ‘scope’ to be the area of accessibility. In terms of scope, the variables declared with &lt;em&gt;var&lt;/em&gt; are globally accessible or visible whereas the variables that are declared using &lt;em&gt;let&lt;/em&gt; are locally accessible or visible within that block. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    for(let i = 0; i &amp;lt; 5; i++) {
    setTimeout(() =&amp;gt; console.log(i))


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above the value of &lt;em&gt;i&lt;/em&gt; will be 0 then 1 then and so on till 4 , thus the value of &lt;em&gt;i&lt;/em&gt; is constrained to the particular iteration. Now let us see what happens in the case of &lt;em&gt;var&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    for(var i = 0; i &amp;lt; 5; i++) {
    setTimeout(() =&amp;gt; console.log(i))


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time the output will be 5 times 5, what happened is that the loop gets executed and the current value gets stored in the global variable i since setTimeout puts a delay to the function console.log(), for every iteration the current value of &lt;em&gt;i&lt;/em&gt; which is 5 is printed.&lt;br&gt;
There are still other things that differentiate these variable declarations but I wanted to keep it at a novice level. I hope I was able to keep things simple for everyone to understand especially for anyone who has just started learning javascript.&lt;br&gt;
So until next time,&lt;br&gt;
Ciao!!!!!!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>FUN with DOM</title>
      <dc:creator>Ayushman Parasar</dc:creator>
      <pubDate>Sun, 08 Dec 2019 15:51:52 +0000</pubDate>
      <link>https://dev.to/ayushmann/fun-with-dom-181e</link>
      <guid>https://dev.to/ayushmann/fun-with-dom-181e</guid>
      <description>&lt;p&gt;Last few days I have been learning JavaScript with the aim of becoming a full stack developer. As I go through this journey, I feel it would benefit me (and hopefully others) if I document my learnings. Thus comes the first installment - DOM (not to be confused with DOMinic Toretto - bad joke? Yeah, probably).&lt;/p&gt;

&lt;p&gt;So what is DOM?&lt;/p&gt;

&lt;p&gt;Dom is nothing but a model of a document where contents can be treated as objects thus every element of the HTML becomes an object. The model here represents how the objects form a tree-like structure. It represents the document as a node tree, where each node represents part of the document.&lt;br&gt;
In simple words, a web page is a document that can be either displayed in the browser window or as the HTML source but it is the same document in both cases. Document Object Model (DOM) represents that same document so it can be manipulated and the content of the page is stored in DOM and can be accessed and manipulated using Javascript. In order to manipulate the element one has to first select that particular element.&lt;/p&gt;

&lt;p&gt;Now, let us see how we can access the contents of a page using DOM(traversing the DOM):&lt;/p&gt;

&lt;p&gt;Accessing the elements of the page can be done using various methods like 'querySelector', 'querySelectorAll',getElementsBy*, etc. For example,&lt;/p&gt;

&lt;p&gt;var elem = document.querySelector('.check');&lt;/p&gt;

&lt;p&gt;Here the document is a predefined object and it represents the entire page that is loaded into the browser. Using this document object we can access various predefined functions and objects. In the above code, we are using querySelector() which returns the first element within the document that matches the specified selector (in this case the class "check").&lt;/p&gt;

&lt;p&gt;Manipulation of Content:&lt;/p&gt;

&lt;p&gt;In the above example, the elem element is an object representing the targeted div having class "check". Now using this elem we can manipulate the contents of that div and even make this div perform a function. Now let us see what we can do with it.&lt;/p&gt;

&lt;p&gt;elem.textContent ="Hello World";&lt;br&gt;
 &lt;br&gt;
In the above code, we have changed the text content which was earlier Lorem to Hello World.&lt;/p&gt;

&lt;p&gt;Let us take another example.&lt;/p&gt;

&lt;p&gt;let buttonObj = document.querySelector('.button');&lt;/p&gt;

&lt;p&gt;Here we are selecting a button by its class and creating an object buttonObj.&lt;/p&gt;

&lt;p&gt;buttonObj.addEventListener('click',displayHello);&lt;/p&gt;

&lt;p&gt;Let me explain what the above code does. The addEventListener() is an inbuilt JavaScript function that takes the event and also a second argument, a function that gets called every time the event is fired. Thus in our case whenever we click on the button, the function displayHello gets called which is declared as follows:&lt;/p&gt;

&lt;p&gt;function DisplayHello(){&lt;br&gt;
   alert("Hello");&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;So every time we click on the button an alert window appears showing a message "Hello World". &lt;/p&gt;

&lt;p&gt;This is just the basic use of DOM, I hope I could provide an insight of what DOM is, how we can access the elements of a web page using DOM and how we can manipulate the contents. For more clarity the following resources can be used:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.developer.mozilla.org"&gt;www.developer.mozilla.org&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.info"&gt;https://javascript.info&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dom</category>
    </item>
  </channel>
</rss>
