DEV Community

SinhueHerrera90
SinhueHerrera90

Posted on

Started using routes helpers on Ruby

So after a couple of months of nos posting anything, I am coming back to post about something I've learned today that I think it will really simplify the way I write/understand code

As a new software developer, I am currently learning Ruby and Ruby on rails, I was taught how to write routes the old way

get("/route_name", {:controller => "controller_name", :action => "action_name"})

Now, after learning about helper methods, I got a lot of things that can be changed for efficiency and save time, for example, now my routes will look like this (as they should)

post "/movies" => "movies#create", as: :movies

Now I learned that ruby automatically uses a prefix to create methods that we can later use on for static routes on our html templates,the syntax looks like this (in this case using the route "/movies" as an example) movies_path (there is also a method we can use with _url ending instead of _path, but that is to be used only for us when sending responses back, usually on our controllers)
For a dynamic route, we can use the same syntax after naming our route, but since it's a dynamic route, it will expect an argument, for example, let's say that in our previous route we have a dynamic route called edit that we named edit_movie_path, we would write on our html template:

edit_movie_path(the argument we want to display)

Naming our routes is an extremely useful feature, because in the future, if we need to change the route name itself, instead of going to each view template where the route is called and look one by one, we can just use our prefixed methods for routing, or we can use the names we give for our dynamic routes.

I also learned that we don't necessarily have to call the whole view template if the action name matches the template name, for example, instead of typing an action called index on a controller called Comments like this:

render({ :template => "comments/index.html.erb"})

We can just simply write it like this:

render "comments/index" without specifying the html.erb, since ruby will take as first format an html.erb (if we were to render a different format, then we would have to write the extension)

What's even better is that we DON'T HAVE TO EVEN CALL THE RENDER METHOD AT ALL if the folder name matches the controller name and the template matches the action name, in this case we would just write the action and the render would happen by itself (we still need to create something to render, of course)

Another thing I learned was how to replace the element from html with a ruby helper method called link_to

Basically, the way it works is like this, we write on out html the Ruby tags <%= link_to "Whatever we want to display as the text for the link, the link%>

Using our previous examples on routing, we can actually call for a route method or link when using the link_to helper method, for example

<%=link_to "Add a new movie", new_movie_path%>

Let's say that we have an active record object for which we want to show the .id method, we don't have to call for it, as ruby will assume that is what we want to retrieve.

We can also drop the .id method whenever we want to retrieve it as Ruby will automatically think that's the information we want to display to the user.

Lastly, I learned the new way of writing structure in Ruby, for example, we can remove {} when the last argument of a method is given, and we can also remove the () if the order of factors wouldn't affect our code.

Overall, this has been an amazing chapter that I am sure will ease my work on future projects, and somehow it's making everything make more sense to me.

Long post, I know but I was pretty excited about it, I'll probably will come back to edit cause I know there's some stuff I could explain better, but for now, I am happy with it

XOXO

Sinhue

Top comments (1)

Collapse
 
heratyian profile image
Ian

Excellent deep dive into helper methods Sinhue!