Basics to Restful Routes

akaaronkim

akaaronkim

Posted on January 22, 2020

Basics to Restful Routes

Understanding how to create routes is essential in Rails. So we are going back to the fundamentals because rails is magical on what it can do for you. Let's take that away and see how rails can interact within.

Alt Text

The main thing though to take away from this is that the Route is such an important part of the application if and especially in these server rendered applications, kind of the type of things, you build when you're building fully in rails. If your browser had no JavaScript you disabled it, the only way a user could ever interact with your application would be through a route. It's this endpoint that is put in the address bar of a browser. So it's really important that you understand that when you're in your address bar, you know this might say Google.com everything after the slash is the only real way that you interact with the application and most the time of course the user doesn't actually type that in your dress they're clicking on a link or they're clicking on a button in a form and that's really just going to another endpoint. This is the only real way a user communicates with your app.

Alt Text

Understanding how to build that from the very beginning is very important. A route is made up of a unique pairing of the method also known like the HTTP verb and the endpoint itself, which is just the the structure of everything after that slash. You can have one unique route the pairs a gap verb with this specific URL now you can put other verbs in front of it that could be put, post, delete, etc. But the unique pairing is very important. And then what a route does is it specifically says go to this controller and (this) action on the controller it's a very that you will you can have multiple routes go to the same controller action but which is generally quite rare but whatever your route is doing it is very specifically going through a controller action.

And then another thing that might always be a apps wish because rails hides it is that it always renders a view. The reason this doesn't seem so obvious is because in a controller on Rails, if you don't say render review it will choose where to render it instead by convention. But you can always choose to render something else entirely that doesn't match the convention. Understanding how to do that and when to do that is very important.

class PostsController < ApplicationController

 def index
   render '/app/views/posts/index.html.erb'
 end
end

Alt Text

Finally the view will always just spit out a new piece of HTML on your in your browser that will have other links that will go to other routes. There was one other piece to cover in that in rails, specifically rails, is that it provides what is called helpers. You have probably heard of
helpers in terms of view helpers and form helpers. Helpers are really just a ruby method a named Ruby method that you call and it will execute a block of code for you. These helpers get attached to your route and it is a way to say, “anytime I want to call this specific route the get posts route” I can use this posts_path and and that will send through to this route. So instead of having to explicitly write out the route you can use the helper.

💖 💪 🙅 🚩
akaaronkim
akaaronkim

Posted on January 22, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related