<?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>Forem: Ayaz</title>
    <description>The latest articles on Forem by Ayaz (@saintaze).</description>
    <link>https://forem.com/saintaze</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%2F65675%2Fd1a2451e-0edc-4fa3-975d-00e73205d7a0.jpg</url>
      <title>Forem: Ayaz</title>
      <link>https://forem.com/saintaze</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/saintaze"/>
    <language>en</language>
    <item>
      <title>Document Your Learning</title>
      <dc:creator>Ayaz</dc:creator>
      <pubDate>Tue, 21 Apr 2020 00:25:52 +0000</pubDate>
      <link>https://forem.com/saintaze/document-your-learning-2nj7</link>
      <guid>https://forem.com/saintaze/document-your-learning-2nj7</guid>
      <description>&lt;p&gt;Documenting your journey can stand you out as a developer and it comes with a host of benefits both personal and professional.&lt;/p&gt;

&lt;h1&gt;
  
  
  A Case For Writing
&lt;/h1&gt;

&lt;p&gt;Writing helps you sharpen your perception and strengthen your observation. When you are writing about what you have learned it forces you to organize your disparate thoughts into a cohesive whole which helps to clarify your own concepts. &lt;/p&gt;

&lt;p&gt;It makes you a better thinker, a better analyst and gives you a more robust thought process system. Writing also strengthens your focus and jogs your memory. It promotes new connections among disconnected ideas and makes a web of knowledge bigger and richer.  &lt;/p&gt;

&lt;p&gt;Your writing also acts as powerful notes that you can refer back to when you want to refresh a concept and since it's written by you it is easy to understand and instantly accessible. &lt;/p&gt;

&lt;p&gt;Writing not only helps you in cementing your knowledge but also elevates you professionally. &lt;/p&gt;

&lt;p&gt;If you are looking to get hired, then having a portfolio of blogs (among other things) to show can give you some extra edge over other job seekers.  And if you already have a job then writing can be a great tool to quickly establish you as an authority in your domain. &lt;/p&gt;

&lt;h1&gt;
  
  
  A Royal Road to Creativity
&lt;/h1&gt;

&lt;p&gt;One very important aspect of writing is that it fosters creativity which helps your mind to fill in the gaps for missing knowledge. You learn to think out of the box, you start to ask questions, you ponder and come up with your own explanations (right or wrong). &lt;/p&gt;

&lt;p&gt;A developer really is creator first, coder second and coding at its heart really is ingenuity wrapped in a cloak of technicality. It is a creative process that externally appears logical. So to be a better coder it makes sense to pay attention to creative development in addition to technical development. &lt;/p&gt;

&lt;p&gt;Unfortunately, most developers never get to learn the benefit of being a creative person. They get bogged down into the rigid and restrictive mindset of what is right, what is wrong, what is fast and what is slow completely ignoring the creative side which has no boundaries, no rules and that is where great ideas and solutions are born.&lt;/p&gt;

&lt;p&gt;So how can you activate creativity? The easiest, cheapest and most accessible way is writing. Working on the creative side helps to excel on the technical side as well. &lt;/p&gt;

&lt;p&gt;By synching your creative (writing) and logical side (coding) you bring about a harmonious synergy in mind and create a system where one side can lean on to the other for solutions if it can't find one on its own. And having such a mental system in place makes you a better-rounded person/coder.&lt;/p&gt;

&lt;h1&gt;
  
  
  Get Into The Habit Of Writing
&lt;/h1&gt;

&lt;p&gt;Take writing as a vital supplement to your learning. Every day, try to spend 10 minutes writing about what you have learned that day. You can begin by writing a small single paragraph about a new concept. &lt;/p&gt;

&lt;p&gt;It doesn't have to be a fancy piece, it doesn't even have to be entirely correct. The key is not to worry too much about what you write but &lt;em&gt;that you do write&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;As a beginner, you may fall into the trap of comparing yourself with others in terms of quality, length, technicality, and originality. But initially, you should rather focus more on developing and refining your art.  &lt;/p&gt;

&lt;p&gt;No matter how shallow or incompetent it may sound, don't be put off by that. The goal is to cultivate the habit of writing and the quality will follow on its own. And once you become more confident, you can open up to sharing it with others. &lt;/p&gt;

&lt;p&gt;By putting it out there you get viewpoints you haven't had the luck to explore before, it may solve an issue, refine a thought, help you see your weakness, reorient your learning and improve your understanding. &lt;/p&gt;

&lt;p&gt;It also lends you community support and you no longer feel like operating in an isolated shell, a vital element to boost your morale.&lt;/p&gt;

&lt;h1&gt;
  
  
  What I Want To Write Already Exists!
&lt;/h1&gt;

&lt;p&gt;You can write whatever floats your boat. But you may think &lt;em&gt;oh geez&lt;/em&gt; there are already a dozen articles, tutorials, blogs on the same topic. &lt;em&gt;Yes&lt;/em&gt;, there might very well be but they are &lt;em&gt;not from you&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;If you want to be a soap or shampoo manufacturer you wouldn't start by thinking &lt;em&gt;oh my&lt;/em&gt; there are already a lot of soaps and shampoos on the market. Yes, there are and even though all of them pretty much do the same thing, wash your hair and body, they are &lt;em&gt;not from you&lt;/em&gt;, they are &lt;em&gt;not your product&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, when you write, don't worry about how many instances (see what I did there?) of the same topic exist, go at it anyways, marinate it nicely and spice it up with your own flavor. &lt;/p&gt;

&lt;p&gt;You write your version of it. Just like there are many sides to the same story, there can be many authors for the same topic.&lt;/p&gt;

&lt;h1&gt;
  
  
  Write To Improve Not To Score Points
&lt;/h1&gt;

&lt;p&gt;Remember to write for your sake first and then for others. And if in the process you build a following and people start to like you and engage with you then that is an honor and a privilege. You can be proud of that. &lt;/p&gt;

&lt;p&gt;But if that doesn't occur it must not discourage you, because you are primarily writing to help yourself. &lt;/p&gt;

&lt;p&gt;You are doing it because it helps you reinforce your concepts and serves as a guidepost to where you stand right now and where you ought to go.&lt;/p&gt;

&lt;p&gt;You must not worry too much about getting likes and comments as writing is a skill and just like with any other skill it takes time to get good at it and like any other skill, it needs practice and dedication. Stick to it and you will see your efforts bloom.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Remember to not take writing as a chore but to have fun while doing it. Be excited when you are about to write, you don't know what your mind is going to reflect on a paper (can be electronic). Do not be dismayed by the results but be thrilled by the effort. &lt;/p&gt;

&lt;p&gt;In due time you may become an ace writer with an audience, an independent voice, and a unique style. And then you would have yet another solid skill to flaunt apart from coding. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be an innovator at heart and use coding to paint your art. &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>todayilearned</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Concerns Of A New Developer</title>
      <dc:creator>Ayaz</dc:creator>
      <pubDate>Wed, 18 Mar 2020 13:12:28 +0000</pubDate>
      <link>https://forem.com/saintaze/the-dilemma-of-a-developer-336h</link>
      <guid>https://forem.com/saintaze/the-dilemma-of-a-developer-336h</guid>
      <description>&lt;p&gt;Lately, I have been seeing similar sentiments from aspiring devs who are not yet in the programming fold but want to jump aboard. &lt;/p&gt;

&lt;p&gt;They ask certain questions and express some fears and concerns that I thought to write a post on so they might find it useful. &lt;/p&gt;

&lt;p&gt;I now present their questions and my opinions as answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) What is the hottest language going forward?
&lt;/h2&gt;

&lt;p&gt;This question is inherently wrong because being a programmer is often like living in the eye of the storm where things are constantly flying around, coming into view and disappearing into the void. &lt;/p&gt;

&lt;p&gt;You have to deal with the chaos regularly. With trends rapidly shifting, nothing is too hot for too long. The question should rather be what area do I enjoy the most.&lt;/p&gt;

&lt;p&gt;I know a few people who caught on the React wave and decided to cash in on it. They learned React and got the job. But after a few months, they were unsatisfied, felt empty and didn't like what they were doing. &lt;/p&gt;

&lt;p&gt;They didn't like dealing with HTML, CSS, UI interaction and other front end stuff, something was amiss, it just didn't jive with their soul. &lt;/p&gt;

&lt;p&gt;Moreover, they also felt silly and inferior taking care of the buttons, the hover effects and the like. So ultimately they switched to the back end and quickly started finding their feet, felt much more comfortable and much happier.&lt;/p&gt;

&lt;p&gt;So one must not see programming as having one king to rule them all. It's more like small independent sovereign kingdoms spread across the programming realm where each empire (front end, data science, hacking, gaming, distributed, backend, mobile) has its own autonomy (market/jobs), its own reigning king (the goto language) and its own politico-social setup (the community, ecosystem, backing by companies, the auxiliary tools). &lt;/p&gt;

&lt;p&gt;These kingdoms also have relationships with each other to promote the exchange of ideas (each domain sharing its expertise, techniques, and people over with the other domain).&lt;/p&gt;

&lt;p&gt;The kings over time can be challenged by a new contender (new trend/language/stack). Some times this disrupter succeeds in overthrowing the king (new tech is the way to go) while at other times the old king digs his heels and quells the rebel (trend dies without making any significant impact). &lt;/p&gt;

&lt;p&gt;Some times, all the infighting fractures the kingdom and breaks it into smaller fiefdoms (multiple rulers). So you can see the programming world is as dramatic and as over the top as Game of Thrones. It is more soap opera than you had thought.&lt;/p&gt;

&lt;p&gt;As there is no one winner, you must explore the programming world a little bit to see where you naturally fit. Going with the hottest doesn't necessarily mean you will enjoy it. Ask people around, research on the internet, watch videos and sample a few areas.&lt;/p&gt;

&lt;p&gt;Whatever you resonate with build your home there. But later if you don't like it then don't be afraid to move on. Discovering what you like is a process and not an epiphany.&lt;/p&gt;

&lt;h2&gt;
  
  
  2) Is programming going to be gone tomorrow?
&lt;/h2&gt;

&lt;p&gt;I hope it does, at least we all can relax for a day and finish all the side hustle that is piling on (kidding). &lt;/p&gt;

&lt;p&gt;Are human calculations and calculators obsolete with the arrival of powerful computers? Is bicycle obsolete due to the advent of cars? You got the point.&lt;/p&gt;

&lt;p&gt;Likewise, the programming won't be obsolete but its nature might change.  But change simply reflects evolution which is natural. &lt;/p&gt;

&lt;p&gt;Just as communication evolved from Morse Code to landlines to pager to feature phones to smartphones to foldable phones etc. We might see rapid advancement in how we do programming but it in itself will still be there. &lt;/p&gt;

&lt;p&gt;Paradigm shifts are inescapable they are not exclusive to programming. They occur in pretty much every industry now. &lt;/p&gt;

&lt;p&gt;So instead of discussing whether it will be over in the next few years, you should rather be asking how best I can make myself adaptable to growing changes around me and in doing so make myself more marketable. &lt;/p&gt;

&lt;p&gt;In my opinion, soon success won't be determined by whether you have picked the winning horse from the get-go but by how quickly and masterfully you can change horses if the one you are sitting on is slowing down or is giving you a loss. That is where the winners and losers will be decided. &lt;/p&gt;

&lt;p&gt;As the pace of technology quickens and we see more revolutions in an ever shorter time span, I feel there will be a big trend moving towards learning the adaptable mindset and how to use it effectively in your profession and your daily life. &lt;/p&gt;

&lt;p&gt;But contrary to popular belief we can see how the evolution of technology has given rise to new fields (ML/AI, IOT, Quantum Computing, Blockchain) which were unheard of before. &lt;/p&gt;

&lt;p&gt;And as technology continue to grow and expand even more tech-driven streams will be unlocked that we don't know yet. So, despite the doom and gloom, I see rather more opportunities than decline. &lt;/p&gt;

&lt;h2&gt;
  
  
  3) There are so many things to learn all the time!
&lt;/h2&gt;

&lt;p&gt;Quite many coders fret over learning this or that. But you should stop looking at coding in terms of frameworks. &lt;/p&gt;

&lt;p&gt;Coders are business people in the sense that they are laying the technological foundation on which the edifice of digital business is erected.&lt;/p&gt;

&lt;p&gt;They are solving business problems via coding. The company that hired you or will hire you has a business problem to crack or a vision where they want to go and frameworks and languages are the tech tickets to get there. &lt;/p&gt;

&lt;p&gt;Languages and frameworks are a means, not the end. These are simply tools even though powerful, sophisticated and at times complicated nevertheless they are but tools to address a problem and reach a goal. &lt;/p&gt;

&lt;p&gt;Coder depression is a real thing and we are all in it together. Most of it stems from the need to learn constantly (in addition to loneliness). But if you see learning a framework as the be-all and end-all then when the new one comes out your mind flips and you get mad.&lt;/p&gt;

&lt;p&gt;Because now there is a new moving target that you got to shoot. A mad rush to chase yet another tool. Back to square one. The frustration builds up, implodes your motivation and slumps you into your chair listlessly.  &lt;/p&gt;

&lt;p&gt;You cannot change the market trends and the direction the programming world is headed in. But you can certainly change how you frame your mind and perceive those changes. There is a nuanced psychological perception at play.  &lt;/p&gt;

&lt;p&gt;Languages and frameworks are steps to reach a goal. If you focus on the steps then when it falls out (framework/language becomes obsolete) you feel someone just pulled the rug from underneath your feet and it may rattle you emotionally. &lt;/p&gt;

&lt;p&gt;You have to see them as tools, disposable and replaceable. And with that mindset when you keep your eyes on the goal rather than the steps, it no longer matters if the step you are standing on is completely different tomorrow.&lt;/p&gt;

&lt;p&gt;It no longer bothers you when the new toy hits the market as getting and playing with the toy is never the goal but to use and do something with it is. &lt;/p&gt;

&lt;p&gt;This small change in perception helps you keep your priorities straight. And you begin to think at a higher level. &lt;/p&gt;

&lt;p&gt;You break free from the narrow thinking of framework wars and think at the level of business, creation, innovation and ponder what you can do to improve, and how you can provide greater value with the help of tech. &lt;/p&gt;

&lt;h2&gt;
  
  
  4) I saw this or that person, he is a genius and I feel so incompetent ergo I can't do this!
&lt;/h2&gt;

&lt;p&gt;The people that you run into and get swept by can be loosely divided into two groups, fake or real. &lt;/p&gt;

&lt;h3&gt;
  
  
  Fake People
&lt;/h3&gt;

&lt;p&gt;The inundation of media is a double-edged sword. With Facebook posts to Instagram pictures to Youtube gurus to blowhard LinkedIn profiles. Everyone wants a big title and image associated with them. &lt;/p&gt;

&lt;p&gt;I see co-founder, entrepreneur, serial entrepreneur titles used liberally. I know some people personally who use the title entrepreneur and founder even though they haven't build a darn thing. &lt;/p&gt;

&lt;p&gt;A lot of people just want to look and sound cool. They put on fake degrees, fake experiences, fake titles just to look competent when all they are doing is being disingenuous. &lt;/p&gt;

&lt;p&gt;I guess having padded digital presence is a new way of saying I got a bigger car. There is a lot of wannabe influencers/experts/showman out there with some real people sprinkled here and there. &lt;/p&gt;

&lt;p&gt;If they are pushing their image too hard chances are they are not genuine in claiming what they are. In my experience, most genuinely successful people keep it low key. &lt;/p&gt;

&lt;p&gt;With perennial connectivity and everyone in your face every second you lose focus and start to compare yourself with every other person, you see on the internet. The result is that you feel insecure, feel defeated and give up doing even before you begin. &lt;/p&gt;

&lt;p&gt;In my view, if anything on the internet makes u feel incompetent don't go there, turn your face away. The last thing you want to lose in today's competitive world is your self-belief and confidence. &lt;/p&gt;

&lt;p&gt;Better alone and be stable than be with someone and have doubts about yourself. But on the other hand, if anyone inspires you, motivates you and fires you up, by all means, tag along.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real people
&lt;/h3&gt;

&lt;p&gt;In this case, you probably are comparing yourself to a person who has been around for a long time and has been in trenches. They probably have years of experience and a lot of hard work and sacrifice behind them which creates the persona you feel awed by. &lt;/p&gt;

&lt;p&gt;The mountain you are overwhelmed with didn't come about in a day. So, this rather comes down to grind and perseverance. If you put in the same amount of work, dedication, and effort, one day you too will be feared like Mr. White (Breaking Bad). &lt;/p&gt;

&lt;p&gt;When you are starting out don't look at how high the mountain is. Just focus on your steps one at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  4) I am not made for it!
&lt;/h2&gt;

&lt;p&gt;When you get the gemstone from ground its never in a form to be used in jewelry. Once it's taken out it's cleansed, cut and shaped before it becomes the sparkling splendor that it does. It takes time to build your place in any field.&lt;/p&gt;

&lt;p&gt;When you are pondering whether it's for you or not, don't treat it as a product you buy in the market, like shoes that you decide on in a few seconds. So, don't make decisions with very little understanding and with no effort put in.&lt;/p&gt;

&lt;p&gt;Rather take some time to research and find about it as much as possible. Definitely try your hand at it, for 2 to 3 months or more if you can. &lt;/p&gt;

&lt;p&gt;Even if you don't enjoy it initially force yourself to do it. Because often your dislike stems from your lack of understanding, your fear, and being overwhelmed by new concepts, new terms and a new way of thinking. But when you get past that phase, you begin to enjoy the field. &lt;/p&gt;

&lt;p&gt;And if even after 3 to 4 months of trying and having given its due, you don't like it at all, by all means, you are free to move on and try something else. &lt;/p&gt;

&lt;h2&gt;
  
  
  Some Bonus Opinion (Double Layered Cheese)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; Another thing to keep in mind is to not start with messy questions like Which is the best language? What are the bad sides of this or that framework? What is good, what is bad? Should I do this or that? &lt;/p&gt;

&lt;p&gt;I call these philosophical questions. When you are starting, they are your biggest enemies and you should keep the judgemental &lt;code&gt;YOU&lt;/code&gt; out of the picture. Just learn the trade in a systematic fashion without getting drawn into philosophical debates. &lt;/p&gt;

&lt;p&gt;And once you are good enough then you can explore the good, the bad, the ugly and fight over it all day long.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; Don't run after the newest shiny toy unless you really have to. If you all of a sudden see the internet coming live with talks, tutorials, forum chats, videos, blogposts, of particular language or framework, that doesn't mean you should drop everything and run after that. &lt;/p&gt;

&lt;p&gt;My rule of thumb always is to give tech some time to mature, see how it goes and how the market reacts to it much like how you trade in stocks. And I said how the market reacts to it not the internet, there is a big difference and you should draw the line between the two. &lt;/p&gt;

&lt;p&gt;Regardless of the internet buzz if it is not practically used by companies on a wider scale and the adoption rate is slow I would leave it for a while. Don't get caught up in the frenzy of newest and the trendiest. &lt;/p&gt;

&lt;p&gt;On the other hand, if your company must use it or its proven to solve a problem more effectively then, by all means, jump on board. Just, don't be driven by internet hype, rather focus on things that give you immediate value. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Those are some of the concerns I see crop up quite a bit, so I thought to share my thoughts on it. There is no right or wrong, this is all personal opinion but you can take it as a guideline, fork these ideas into your own mind and modify them to make them work for your situation. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>motivation</category>
      <category>beginners</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Rendering list items in Ruby on Rails 6</title>
      <dc:creator>Ayaz</dc:creator>
      <pubDate>Thu, 14 Nov 2019 02:33:05 +0000</pubDate>
      <link>https://forem.com/saintaze/rendering-list-items-in-ruby-on-rails-6-48ae</link>
      <guid>https://forem.com/saintaze/rendering-list-items-in-ruby-on-rails-6-48ae</guid>
      <description>&lt;p&gt;In web apps, we are all too familiar with the concept of looping over list items and displaying them to the user. Be it a list of products, users, articles, or some other entity.&lt;/p&gt;

&lt;p&gt;In rails, we can display a list of items in the following ways. &lt;/p&gt;

&lt;p&gt;1) By putting a markup of each item in a file that also contains some other markup. Basically, the item markup is not in its own separate file.&lt;/p&gt;

&lt;p&gt;2) By putting item markup into its own separate file (partial).&lt;/p&gt;

&lt;p&gt;In this second option, we have two convenient ways to render each item in the list.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;loop over the list and pass each item as a local variable into the item partial.&lt;/li&gt;
&lt;li&gt;pass the entire list directly to the &lt;code&gt;render&lt;/code&gt; method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see all of it in a little more detail.&lt;/p&gt;

&lt;h1&gt;
  
  
  Putting item markup in a file that also contains some other markup
&lt;/h1&gt;

&lt;p&gt;Imagine that we are running a men's suiting store and we have a suitings page on which we display &lt;em&gt;surprisingly&lt;/em&gt; all the men suits via &lt;code&gt;suitings.html.erb&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome to the Men's Clothing&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;We offer the finest men's suiting West of Atlantic&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;While you are here paint the digital town red!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="vi"&gt;@suitings&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;suiting&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;li&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="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;type&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fabric&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;category&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;price&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;/li&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;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;We hope you won't leave, without giving comfort to your body that it deserves!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

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



&lt;p&gt;This is the first and the most straightforward way to display a list of items. We have &lt;code&gt;@suitings&lt;/code&gt; instance variable coming from the &lt;code&gt;controller#action&lt;/code&gt;. We loop over the list and display each item one by one.&lt;/p&gt;

&lt;h1&gt;
  
  
  Problem with this approach
&lt;/h1&gt;

&lt;p&gt;Currently, the markup for each list item housed in &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; tag is only a few lines long, so it doesn't matter much. &lt;/p&gt;

&lt;p&gt;But who is to say that our store won't become a big hit tomorrow and then we would have to render much more complicated list item markup, several lines long.&lt;/p&gt;

&lt;p&gt;The product card may soon grow to contain checkboxes, radio buttons, drop-down, text-field, and other information about the product. Such a markup would naturally swell the list item template. &lt;/p&gt;

&lt;p&gt;Also currently we are dumping the list item template in a &lt;code&gt;suitings.html.erb&lt;/code&gt; file that also has its own markup, such as the &lt;code&gt;&amp;lt;h1&amp;gt; &amp;lt;h2&amp;gt; and &amp;lt;p&amp;gt;&lt;/code&gt; tags. Perhaps this page will also grow to contain more markup. &lt;/p&gt;

&lt;p&gt;Thus putting everything in one file can create a lot of mess and with time can become a maintenance nightmare. &lt;/p&gt;

&lt;p&gt;That is where the second form of list items rendering comes into play, where we separate the code out into its own partial.&lt;/p&gt;

&lt;p&gt;But before we look at that, it helps to have a little detour about the partials. &lt;/p&gt;

&lt;h1&gt;
  
  
  What component is to the frontend &lt;em&gt;partial&lt;/em&gt; is to the backend.
&lt;/h1&gt;

&lt;p&gt;If you have done any front-end dev with Vue, Angular or React you can think of a &lt;em&gt;partial&lt;/em&gt; as a component. &lt;/p&gt;

&lt;p&gt;For instance, you can have a form, header, footer, product-card, table, etc. markup littered here and there in your Rails app. They can all be converted into their own standalone partials. &lt;/p&gt;

&lt;p&gt;These partials can be reused in multiple places very much like front-end components. &lt;/p&gt;

&lt;p&gt;You can even design these partials to be generic so that they can respond to variables being passed in from the outside. In doing so you will be defining a public API to communicate with the partial and thus can customize it according to the context in which it is used. &lt;/p&gt;

&lt;p&gt;We will look at a brief example of that at the end.&lt;/p&gt;

&lt;p&gt;Having mentioned that, let's get back and look at the second way of rendering list items.&lt;/p&gt;

&lt;h1&gt;
  
  
  Putting Item markup in its own separate partial
&lt;/h1&gt;

&lt;p&gt;Now we will create a separate partial &lt;code&gt;_suiting.html.erb&lt;/code&gt; and put the relevant markup in there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;li&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="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;type&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fabric&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;category&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;price&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;/li&amp;gt;&lt;/span&gt;

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



&lt;p&gt;Now we can display a list of suitings in the &lt;code&gt;suitings.html.erb&lt;/code&gt; with the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="vi"&gt;@suitings&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;suiting&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
    &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="n"&gt;suiting&lt;/span&gt; &lt;span class="cp"&gt;%&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;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, we are passing &lt;code&gt;suiting&lt;/code&gt; as a local variable inside of the &lt;code&gt;_suiting.html.erb&lt;/code&gt; partial.&lt;/p&gt;

&lt;p&gt;But how does rails know which partial to use? We didn't tell that to the &lt;code&gt;render&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Rails figures that out on its own. It looks at the &lt;code&gt;suiting&lt;/code&gt; variable and determines that it belongs to the &lt;code&gt;Suiting&lt;/code&gt; class. &lt;/p&gt;

&lt;p&gt;So inside of the &lt;code&gt;views&lt;/code&gt; directory, it looks for the &lt;code&gt;suitings&lt;/code&gt; directory, inside of that it looks for &lt;code&gt;_suiting.html.erb&lt;/code&gt; file.&lt;code&gt;views/suitings/_suiting.html.erb&lt;/code&gt;. Rails does that all automatically.&lt;/p&gt;

&lt;p&gt;With that, we have a cleaner code. Now we have our suiting template housed in its own partial and if tomorrow it grows and gets complicated it won't pollute other places.&lt;/p&gt;

&lt;p&gt;But then there is even a better and lighter syntax to render a list of items. You don't even have to define the loop construct. You can directly pass the list to the &lt;code&gt;render&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="vi"&gt;@suitings&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;

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



&lt;p&gt;Here again, rails will look at each item in the &lt;code&gt;@suitings&lt;/code&gt; list, determine that it is instantiated from the &lt;code&gt;Suitings&lt;/code&gt; class and will look  for the &lt;code&gt;_suiting_html.erb&lt;/code&gt; partial and use that to render each item in the list. &lt;/p&gt;

&lt;p&gt;Here the instance variable name is &lt;code&gt;@suitings&lt;/code&gt;. But it can be any name of your choice. You can name it &lt;code&gt;@men_clothes&lt;/code&gt; and it would work just the same because each item in the collection is still of type &lt;code&gt;Suiting&lt;/code&gt; or instantiated from the&lt;code&gt;Suiting&lt;/code&gt; class. So Rails would look for the &lt;code&gt;_suiting_html.erb&lt;/code&gt; partial to render each item.&lt;/p&gt;

&lt;h1&gt;
  
  
  Making Suiting Partial/Component reusable
&lt;/h1&gt;

&lt;p&gt;In closing, let's see how we can generalize &lt;code&gt;_suiting.html.erb&lt;/code&gt; so that it can work with any product list such as sweaters, socks, pants and not only with suitings.&lt;/p&gt;

&lt;p&gt;Firstly, we will rename our partial as &lt;code&gt;_product.html.erb&lt;/code&gt; to make it more generic and make it work with any kind of product list. &lt;/p&gt;

&lt;p&gt;Also, we will move it into a shared directory &lt;code&gt;shared/_product.html.erb&lt;/code&gt; because now it will be shared or reused in multiple places (putting in &lt;code&gt;shared&lt;/code&gt; directory is not required but it's a common convention).&lt;/p&gt;

&lt;p&gt;Secondly, we will have to refactor the markup as well&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;li&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="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;type&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fabric&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;category&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&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;div&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;price&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;/li&amp;gt;&lt;/span&gt;

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



&lt;p&gt;Here we are using &lt;code&gt;product&lt;/code&gt; as the local variable, we will see why.&lt;/p&gt;

&lt;p&gt;Now if we want to render a list of say @shorts we can do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;partial: &lt;/span&gt;&lt;span class="s2"&gt;"shared/product"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;collection: &lt;/span&gt;&lt;span class="vi"&gt;@shorts&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;      
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we have passed a configuration hash to the &lt;code&gt;render&lt;/code&gt; method mentioning which list we want to render in our case &lt;code&gt;@shorts&lt;/code&gt; using which partial in our case &lt;code&gt;_product.html.erb&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Inside the partial, each list item will be available under the same name as the partial name. Since, in our case, the partial name is &lt;code&gt;_product.html.erb&lt;/code&gt;, each item inside the partial will be called &lt;code&gt;product&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;There are three main ways to render lists inside the Rails templates. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mixing item markup with another markup (not having its own separate file).&lt;/li&gt;
&lt;li&gt;Creating a separate partial for item markup.&lt;/li&gt;
&lt;li&gt;Generalizing the partial so that it can be reused in multiple places and work with any list. &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ror</category>
      <category>rails</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
    <item>
      <title>A guide to creating forms in Ruby on Rails 6 </title>
      <dc:creator>Ayaz</dc:creator>
      <pubDate>Sun, 27 Oct 2019 18:18:40 +0000</pubDate>
      <link>https://forem.com/saintaze/a-guide-to-creating-forms-in-ruby-on-rails-6-49do</link>
      <guid>https://forem.com/saintaze/a-guide-to-creating-forms-in-ruby-on-rails-6-49do</guid>
      <description>&lt;p&gt;In any app, forms serve the most primary way for data creation and modification and Ruby on Rails (from here on called Rails) app is no different. &lt;/p&gt;

&lt;p&gt;In this guide, we will look at different ways of creating forms in Rails.  &lt;/p&gt;

&lt;h1&gt;
  
  
  A High-Level Overview
&lt;/h1&gt;

&lt;p&gt;At a high level, there are two ways of creating forms in Rails.&lt;/p&gt;

&lt;p&gt;1) We can create the entire form by hand using HTML just as we would in any static HTML file.&lt;/p&gt;

&lt;p&gt;2) Alternatively, we can use a powerful &lt;code&gt;form_with&lt;/code&gt; helper that makes our lives much easier. This helper can have the following use cases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It can be used to set up a form for a resource that is associated with an Active Record model meaning it has REST routes and also a database model. It can be both for resource creation or update. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It can be used to create a form for a resource that doesn't have any Active Record model, meaning it has REST routes but no database model.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It can be used for setting up a form having no resource and Active Record model, meaning there are neither REST routes nor any database model. Such a form can also be created by hand as point 1 illustrates but using &lt;code&gt;form_with&lt;/code&gt; makes it easier still. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's explore form creation in little more detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Form created with regular HTML markup
&lt;/h2&gt;

&lt;p&gt;If you have done any HTML, then this one is the simplest and the most straightforward way of creating a form in Rails.  Here the form is created with the usual HTML markup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Email&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Password&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Sign up"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In such a form you define everything by yourself and don't take any help from Rails. You have to declare &lt;code&gt;method="post"&lt;/code&gt; and set the form submission route &lt;code&gt;action="/users"&lt;/code&gt; yourself. So far there is nothing Rails specific here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Form Created with &lt;code&gt;form_with&lt;/code&gt; method (The Rails Way)
&lt;/h2&gt;

&lt;p&gt;Rails conveniently offer us a &lt;code&gt;form_with&lt;/code&gt; method to help us in creating a form. With this method, we leverage the Rails magic. &lt;/p&gt;

&lt;p&gt;Let's try to understand how this method works. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;form_with&lt;/code&gt; in action
&lt;/h2&gt;

&lt;p&gt;Suppose we have a User model with email and password attributes. If we want to create a user signup form (resource creation) this is how the form would look like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= form_with(model: @user, local: true) do |f| %&amp;gt;

  &amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;label&lt;/span&gt; &lt;span class="ss"&gt;:email&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="sx"&gt;%= f.email_field :email %&amp;gt;

  &amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;label&lt;/span&gt; &lt;span class="ss"&gt;:password&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="sx"&gt;%= f.password_field :password %&amp;gt;

  &amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt; &lt;span class="s2"&gt;"Sign Up"&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="sx"&gt;% end &lt;/span&gt;&lt;span class="o"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When erb is converted to HTML through Rails asset pipeline it produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt; &lt;span class="na"&gt;accept-charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"user_email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Email&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"user[email]"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"user_email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"user_password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Password&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"user[password]"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"user_password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"commit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Sign Up"&lt;/span&gt; &lt;span class="na"&gt;data-disable-with=&lt;/span&gt;&lt;span class="s"&gt;"Sign Up"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have skipped the input &lt;code&gt;type="hidden"&lt;/code&gt; &lt;code&gt;name="authenticity_token"&lt;/code&gt; tag which Rails add automatically to every form to prevent CSRF attacks.&lt;/p&gt;

&lt;p&gt;Let's dissect the form bit by bit!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;form_with&lt;/code&gt; is a method to which we have provided a configuration options hash. &lt;code&gt;model: @user&lt;/code&gt; defines for which model we want to create the form. Or in other words which object will drive the form creation. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;local: true&lt;/code&gt; configures the form submission request to be local form request. In the absence of this option, Rails configures the form to send a remote XHR request on submission.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;form_with&lt;/code&gt; automatically sets the &lt;code&gt;method="post"&lt;/code&gt; and the submission route &lt;code&gt;action="/users"&lt;/code&gt;. But how do Rails know which route to submit the form to and what method (HTTP Verb) to use? &lt;/p&gt;

&lt;p&gt;Let's understand this now.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;form_with&lt;/code&gt; method with &lt;code&gt;POST&lt;/code&gt; request (resource creation)
&lt;/h2&gt;

&lt;p&gt;If the user is signing up for the first time meaning the user doesn't exist in the database yet, then the form will be served by the &lt;code&gt;User#new&lt;/code&gt; action in which we initialize &lt;code&gt;@user = User.new&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;@user&lt;/code&gt; will be available in the &lt;code&gt;User#new&lt;/code&gt; view which contains sign up form. Currently, all &lt;code&gt;@user&lt;/code&gt; attributes are set to &lt;code&gt;nil&lt;/code&gt;, since we didn't initialize attributes in &lt;code&gt;User.new&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thus when we set &lt;code&gt;model: @user&lt;/code&gt;, Rails checks if any of the attributes of &lt;code&gt;@user&lt;/code&gt; have a value other than &lt;code&gt;nil&lt;/code&gt;. If all the fields are &lt;code&gt;nil&lt;/code&gt; then it infers this user (resource) will be created for the first time, so it correctly sets the &lt;code&gt;method="post"&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But what about the route? &lt;/p&gt;

&lt;p&gt;In order to set the route rails calls &lt;code&gt;@user.class&lt;/code&gt; to determine the Model class name which is &lt;code&gt;User&lt;/code&gt;, looks for a similarly named Controller class &lt;code&gt;User&lt;/code&gt; and maps to the correct action at which the user will be created, which would be &lt;code&gt;User#create&lt;/code&gt; for which method is &lt;code&gt;POST&lt;/code&gt; and route is &lt;code&gt;/users&lt;/code&gt;. And that is how it finds out where to submit this form. &lt;/p&gt;

&lt;p&gt;If you name instance variable something else, such as &lt;code&gt;@foobar = User.new&lt;/code&gt; and set &lt;code&gt;model: @foobar&lt;/code&gt; it would still correctly determine the submission route by calling &lt;code&gt;@foobar.class&lt;/code&gt; which would still be &lt;code&gt;User&lt;/code&gt;. So instance variable name doesn't matter. &lt;/p&gt;

&lt;p&gt;But if the Rails automatically inferred route is not where you want to submit the form, you can always use &lt;code&gt;url: {whatever the submission route}&lt;/code&gt;. With this attribute to the &lt;code&gt;form_with&lt;/code&gt; you have full control over the submission route, in case you need it.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;form_with&lt;/code&gt; method with &lt;code&gt;PATCH&lt;/code&gt; request (resource update)
&lt;/h2&gt;

&lt;p&gt;The second case is where you are creating a user edit form. Here when you click a particular user link that needs an edit, the request will be mapped to &lt;code&gt;User#edit&lt;/code&gt; where you will fetch the user as &lt;code&gt;@user = User.find(params[:id])&lt;/code&gt;. This &lt;code&gt;@user&lt;/code&gt; will be available in the &lt;code&gt;User#edit&lt;/code&gt; view.&lt;/p&gt;

&lt;p&gt;But since this time &lt;code&gt;@user&lt;/code&gt; attributes have values, rails prepopulate corresponding form fields with available values taking care of not populating password field, so it filters that out. &lt;/p&gt;

&lt;p&gt;Also because &lt;code&gt;@user&lt;/code&gt; attributes have values (even if one attribute has a value other than nil) rails infers that it is not a new resource meaning you are not creating it for the first time but instead you are looking to modify an existing resource. &lt;/p&gt;

&lt;p&gt;So it sets up the route properly by again calling the &lt;code&gt;@user.class&lt;/code&gt; to determine Model, find its namesake Controller &lt;code&gt;User&lt;/code&gt; and map to &lt;code&gt;User#update&lt;/code&gt; which deals with updating a user, having the route &lt;code&gt;users/:id&lt;/code&gt; with a &lt;code&gt;PATCH&lt;/code&gt; request.&lt;/p&gt;

&lt;p&gt;But, there is a problem the browser doesn't natively support the &lt;code&gt;PATCH&lt;/code&gt; request it only supports &lt;code&gt;POST&lt;/code&gt; AND &lt;code&gt;GET&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Rails achieve that by inserting the following tag inside of the form&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"_method"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"patch"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;How does that work? &lt;/p&gt;

&lt;p&gt;&lt;code&gt;POST&lt;/code&gt; simply allows the browser to send data to the server. Form submission piggybacks that to transport data to the server but after the data is transferred it needs to tell Rails that it's not a &lt;code&gt;POST&lt;/code&gt; data but a &lt;code&gt;PATCH&lt;/code&gt; data and it accomplishes that by using such an input tag.&lt;/p&gt;

&lt;p&gt;It simply acts as a label to tell Rails that it's a patch request and use incoming data as an edit data, not a creation data. &lt;/p&gt;

&lt;p&gt;When such a request hits the Rails app, it looks at the form find the hidden input field with a &lt;code&gt;value="patch"&lt;/code&gt; and routes it to the &lt;code&gt;User#update&lt;/code&gt; which handles &lt;code&gt;PATCH&lt;/code&gt; request. So a lot of work is done by Rails. &lt;/p&gt;

&lt;p&gt;Let's take some other intricacies about the &lt;code&gt;form_with&lt;/code&gt; method&lt;/p&gt;

&lt;h3&gt;
  
  
  Form submitting to itself (GET and POST route are the same)
&lt;/h3&gt;

&lt;p&gt;If you create a random instance variable such as &lt;code&gt;@have_no_class&lt;/code&gt; that has no associated Model. So when Rails try to determine the submission route it calls &lt;code&gt;@have_no_class.class&lt;/code&gt; it gets &lt;code&gt;NilClass&lt;/code&gt; and since it couldn't correctly infer the submission route for this resource it would set the route to itself. &lt;/p&gt;

&lt;p&gt;So if you landed on the page with &lt;code&gt;GET&lt;/code&gt; request to &lt;code&gt;'/signup'&lt;/code&gt; then it would submit the form to itself at &lt;code&gt;'/signup'&lt;/code&gt; with &lt;code&gt;POST&lt;/code&gt;. Unless the Rails did actually determine the route and it turned out to be the same as the &lt;code&gt;GET&lt;/code&gt; route.&lt;/p&gt;

&lt;h3&gt;
  
  
  form_with gives a FormBuilder object
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;form_with&lt;/code&gt; takes an options hash but it also takes a block and passes a FormBuilder object as a block variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= form_with(model: @user, local: true) do |f| %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;|f|&lt;/code&gt; is the FormBuilder object (you can name it anything you want) that helps us in setting up form fields. It has methods for creating all the form elements such as radio button, text-area, check-box, input etc.&lt;/p&gt;

&lt;p&gt;So if you want to create an input of &lt;code&gt;type=password&lt;/code&gt; you would do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= f.label :password %&amp;gt;
&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;password_field&lt;/span&gt; &lt;span class="ss"&gt;:password&lt;/span&gt; &lt;span class="o"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It will produce&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"user_password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Password&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"user[password]"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"user_password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

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



&lt;p&gt;Here we gave &lt;code&gt;:password&lt;/code&gt; symbol as an argument to the &lt;code&gt;f.label&lt;/code&gt; method which it uses to set up the label text and &lt;code&gt;for="user_password"&lt;/code&gt;. &lt;code&gt;for&lt;/code&gt; is set to &lt;code&gt;user_password&lt;/code&gt; and not &lt;code&gt;password&lt;/code&gt; because we set &lt;code&gt;model: @user&lt;/code&gt; so it prefixes it with the name of the model. &lt;/p&gt;

&lt;p&gt;Similarly in the input tag for password, it sets the &lt;code&gt;id="password"&lt;/code&gt;, &lt;code&gt;type="password"&lt;/code&gt; and &lt;code&gt;name="user[password]"&lt;/code&gt;. In order to understand &lt;code&gt;user[password]&lt;/code&gt; we have to look at the &lt;code&gt;params hash&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A look at the &lt;code&gt;Params&lt;/code&gt; hash
&lt;/h3&gt;

&lt;p&gt;You have to be mindful of the fact the Rails use the &lt;code&gt;params&lt;/code&gt; hash for sending any data that is being transferred with the request, be it dynamic URL segment like &lt;code&gt;/:id&lt;/code&gt; or query param like &lt;code&gt;?page=1&lt;/code&gt; or the data sent via &lt;code&gt;POST&lt;/code&gt; or &lt;code&gt;PATCH&lt;/code&gt; request, all are available inside the &lt;code&gt;params&lt;/code&gt; hash. In simple words params contain some information about the incoming request. &lt;/p&gt;

&lt;p&gt;What Rails does is that it sets up &lt;code&gt;user&lt;/code&gt; hash inside of the &lt;code&gt;params&lt;/code&gt; hash and inside the &lt;code&gt;user&lt;/code&gt; hash it sets up all the form name, value pairs. &lt;/p&gt;

&lt;p&gt;When the form is submitted it takes all the form values, stuffs them in the &lt;code&gt;user&lt;/code&gt; hash of the params hash and sends it to the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="ss"&gt;params: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="ss"&gt;user: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="s1"&gt;'foobar@baz.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="s1"&gt;'foobar'&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;That is why you see the &lt;code&gt;name="user[password]"&lt;/code&gt; because it is setting up the &lt;code&gt;password&lt;/code&gt; field on the &lt;code&gt;user&lt;/code&gt; hash and setting its value to whatever is typed into the field. But this is all done behind the scenes by Rails.&lt;/p&gt;

&lt;p&gt;Now coming back to the FormBuilder object &lt;code&gt;|f|&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can additionally set up any other attribute on input password tag (or any other form field) by passing in the options hash such as setting up the &lt;code&gt;class&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= f.password_field :password, class: 'form-control' %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;FormBuilder object, in our case &lt;code&gt;|f|&lt;/code&gt; has convenient methods to which you pass configurations and it produces the desired form element for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;form_with&lt;/code&gt; method for a non-model resource
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;form_with&lt;/code&gt; can also be used to set up a form for a resource that has no Model. It means it has some or all of the REST routes but no Model to persist the data. A classic example would be creating Session as a REST resource but with no Model, because data is sent to the browser and not stored on the server.&lt;/p&gt;

&lt;p&gt;In such a form creation you don't say &lt;code&gt;model: @session&lt;/code&gt; because there is no Model to instantiate the object which can be passed into view. Instead, we use &lt;code&gt;scope: 'session'&lt;/code&gt;. This does all the Rails magic steps as discussed earlier.&lt;/p&gt;

&lt;p&gt;Before we have been using &lt;code&gt;model&lt;/code&gt; attribute now we use &lt;code&gt;scope&lt;/code&gt;. We use &lt;code&gt;model&lt;/code&gt; when we have the Model whose instance is going to drive the creation of the form.&lt;/p&gt;

&lt;p&gt;With Session since we don't have a Model we don't use &lt;code&gt;model&lt;/code&gt; attribute. Instead, we use &lt;code&gt;scope&lt;/code&gt; that sets up the &lt;code&gt;session&lt;/code&gt; hash inside of &lt;code&gt;params&lt;/code&gt; hash and all the form values will be stuffed inside of the &lt;code&gt;session&lt;/code&gt; hash.&lt;/p&gt;

&lt;p&gt;Since this time we don't have an instance variable to determine the class and the route, the form submits to itself (same route it is served from).&lt;/p&gt;

&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If you have a form to either create or update a resource that happens to have an associated Model as well, use &lt;code&gt;from_with&lt;/code&gt; and set &lt;code&gt;model: {whatever the instance varaible}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you have a form for a resource that does not have an associated Model, use &lt;code&gt;from_with&lt;/code&gt; and set &lt;code&gt;scope: {whatever the scope name you want in params hash}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you want to create a non-resource form, you can do so with a &lt;code&gt;form_with&lt;/code&gt; in which case you would have to define &lt;code&gt;url: '{whatever form submission route}'&lt;/code&gt;. Or you can create the form by hand, the good-ol way.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ror</category>
      <category>rails</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
