Routing Basics: Ruby on Rails for Front-End Developers

This guest post is by Miles Matthias, who in between trips and sips, usually with planes and bourbon, enjoys talking to people about difficult challenges and tapping on his keyboard to help solve them. He moved to Boulder in January of 2012, after meeting his future wife and picking up degrees in Computer Science and Information Assurance in Omaha, Nebraska. Boulder was his ninth move after growing up in Virginia, Kansas, Illinois, and Indiana, and would be more than happy if Colorado became his permanent home. With experience in network security monitoring, vulnerability assessments, red hat teaming, bash scripting, and web and iOS development, he enjoys building simple and beautiful applications, contributing to the open source community, and writing about his life experiences.

Miles Matthias Here’s an excerpt from my book’s chapter on routing, and then I’ll give you some examples:

Routes config

Remember that bit about convention over configuration? Well rails definitely prefers configuration to convention in one instance – routes. In the file config/routes.rb, you’ll find direction given to the rails router on which controller method to call for a given request. Here’s a simple route declaration:

match 'articles', to: 'articles#index'

If a user makes a request to yoursite.com/articles, the rails router will look for a controller called ArticlesController and if there’s a method called index, it will call it, passing along the information in the request. Then it’s up to the controller to respond.

Resources

You’ll also see what rails calls resources in the routes file, which promotes the convention over configuration pattern. Resources handle the 85% case where your application is representing standard RESTful CRUD operations on a ‘thing’, such as an article. The above example route could be replaced with:

resources :articles

which would then mandate that the method names in the matching controller match the rails resourceful actions.

Examples

If we had the following in your config/routes.rb file:

resources :articles

and our /app/controllers/articles_controller.rb file looked like this:

class ArticlesController < ApplicationController
    def index
        @articles = Article.all
    end

    def show
        @article = Article.find(params[:id])
    end

    def edit
        @article = Article.find(params[:id])
    end
end

Here’s the list of what HTTP Verbs and URL combinations match to what methods inside a controller:


(Credit: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions)

Notice in the controller that we didn’t call render at all. That’s because Rails automatically attempts to render a view that has a filename of the same name of the method called in the controller. If it can’t find one, it will default to the index.html view for the controller, or the application index.html.

For example, if someone wanted to get an article at http://mywebsite.com/articles/1234, Rails would call the show method in our controller with 1234 as the id param. That method (above) will find the article that matches that id and then rails will call render on a view in /app/views/articles/show.html.erb. Here’s an example of what that view might look like:

<h1><%= @article.title %></h1>

<h2>by <%= @article.author %></h2>

<p>
<%= @article.body %>
</p>

If you’d like to learn more about how Ruby on Rails’ front-end stack works or you’re a front-end developer needing to do some work in Ruby on Rails, check out my book, “Ruby on Rails Explained for Front-End Developers”.

Feel free to ask questions and give feedback in the comments section of this post. Thanks!

Technorati Tags: , ,

Posted by Miles Matthias

Follow me on Twitter to communicate and stay connected

1 comment

Registrations are now open for RubyLearning’s popular Ruby programming course. This is an intensive, online course for beginners that helps you get started with Ruby programming. The course starts on Saturday, 8th Feb. 2014 and runs for seven weeks.

Course Fee and Discount

Please create a new account first and then pay US$ 34.95 (for the first 10 participants, after which the course fee will be US$ 69.95) by clicking on the PayPal button Paypal

Note: If you do not have a PayPal account and would like to pay by credit card on PayPal then do email me at satish [at] rubylearning.org.


Download ‘Advice for Ruby Beginners’ as a .zip file.

Here is what Sandra Randall (Butler), a participant who just graduated, has to say – “You kindly offered me the opportunity to join your Ruby course. I’m new to development and found the course, even though basic for programmers, a little tricky for me. I managed to complete all of the assessments and really learnt a lot. Thank you very much for the opportunity. It has really given me the push I needed to learn Ruby and I’m currently treading my way through both the pickaxe and Agile Development books and enjoying it. I’ve recently been offered a position as a Junior Systems Developer at a local Software house in South Africa – all thanks to the push you gave me which gave me the motivation and drive to get going.”

What’s Ruby?

Ruby

According to http://www.ruby-lang.org/en/ – “Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. Ruby’s elegant syntax is natural to read and easy to write.”

Yukihiro Matsumoto, the creator of Ruby, in an interview says -

I believe people want to express themselves when they program. They don’t want to fight with the language. Programming languages must feel natural to programmers. I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby.

What Will I Learn?

In the Ruby programming course, you will learn the essential features of Ruby that you will end up using every day. You will also be introduced to Git, GitHub, HTTP concepts, RubyGems, Rack and Heroku.

Some Highlights

RubyLearning’s IRC Channel

Some of the mentors and students hang out at RubyLearning’s IRC (irc.freenode.net) channel (#rubylearning.org) for both technical and non-technical discussions. Everyone benefits with the active discussions on Ruby with the mentors.

Google Hangouts

There is a Hangout Event that is open for students, for drop-in hangouts where students can pair program with mentors or with each other. This is often where you can get help with your system, editor, and general environment. Anything that can help you with your coding environment that you are having problems with are usually discussed interactively here.

Git Repositories

Shared (private) repositories available for those that want to learn git and the revision controlled programming workflow. This allows students that want to collaborate while learning. This is a great way to record your progress while learning Ruby.

eBook

The course is based on the The Ultimate Guide to Ruby Programming eBook. This book is priced at US$ 9.95. However, the Kindle edition of the eBook is available for US$ 6.

Challenges and Side Tracks

This is course material not found in the RubyLearning Study Notes nor in the E-Book! Depending on participation levels, we throw a Ruby coding challenge in the mix, right for the level we are at. We have been known to give out a prize or two for the ‘best’ solution.

Who’s It For?

A beginner with some knowledge of programming.

You can read what past participants / online magazines have to say about the course.

Mentors

Satish Talim, Michael Kohl, Satoshi Asakawa, Victor Goff III and others from the RubyLearning team.

Dates

The course starts on Saturday, 8th Feb. 2014 and runs for seven weeks.

How do I register and pay the course fees?

  • You can pay the course fees either by Paypal or send cash via Western Union Money Transfer or by bank transfer (if you are in India). The fees collected helps RubyLearning maintain the site, this Ruby course, the Ruby eBook, and provide quality content to you.

To pay the Course Fee:

Please create a new account first and then pay US$ 34.95 (for the first 10 participants, after which the course fee will be US$ 69.95) by clicking on the PayPal button Paypal

How does the course work?

For details on how the course works, refer here.

At the end of this course you should have all the knowledge to explore the wonderful world of Ruby on your own.

Remember, the idea is to have fun learning Ruby.

Technorati Tags: , , ,

Posted by Satish Talim

Follow me on Twitter to communicate and stay connected

Be the first to comment