DEV Community

peabody00
peabody00

Posted on • Updated on

Ruby on Rails, Flatiron Mod3 Project

Ruby on Rails

Module 3 for Flatiron School builds on the previous lessons of Sinatra and ActiveRecord.

Project Idea

I decided to do a simple college website for my project. I call it Chesapeake Clown College. The idea is that two types of users (teachers and students) can log into the website and manage different things. A teacher login can create new courses and activities for the student. The student login can look at the courses and assign them to their schedule. They can also join different activities.

Here is a link to the project.

Important Lesson

I think one of the biggest lessons I got from this module involves how Rails makes routing much easier. With Sinatra the routes had to be explicitly stated in the controllers for things to run properly. Rails simplifies this by declaring all the routes in a file called routes.rb.

The routes can be described in several different ways in this file but the easiest way is to simply declare resources for the different models.

Rails.application.routes.draw do
  get '/login' => 'sessions#new'
  post '/login' => 'sessions#create'
  get '/teacherlogin' => 'sessions#teacher_new'
  post '/teacherlogin' => 'sessions#teacher_create'
  post '/logout' => 'sessions#destroy'
  get '/auth/:provider/callback' => 'sessions#omniauth'
  root 'welcome#index'
  resources :activities
  resources :courses
  resources :schedules
  resources :students_activities
  resources :teachers do
    resources :courses
    resources :activities
  end
  resources :students do
    resources :schedules
    resources :students_activities
  end
  resources :sessions
end
Enter fullscreen mode Exit fullscreen mode

This is the code for the routes.rb from my project. I don't know if it's perfectly correct but it works in my project.

The advantage of declaring routes this way is that resources :activities allows Rails to know the routes for the Activities controller automatically.

Running rails routes gives us this result for Activities.

       Prefix Verb   URI Pattern                          Controller#Action
   activities GET    /activities(.:format)                activities#index
              POST   /activities(.:format)                activities#create
 new_activity GET    /activities/new(.:format)            activities#new
edit_activity GET    /activities/:id/edit(.:format)       activities#edit
     activity GET    /activities/:id(.:format)            activities#show
              PATCH  /activities/:id(.:format)            activities#update
              PUT    /activities/:id(.:format)            activities#update
              DELETE /activities/:id(.:format)            activities#destroy
Enter fullscreen mode Exit fullscreen mode

With just the simple declaration of resources :activities all RESTful CRUD routes are created for me.

This also creates helper methods that can be used in my code. If I need to link a route I just take the prefix from above and add _path to it to create a link. activities_path will take me to the INDEX route for activities. It is a simple yet elegant way to manage routing.

Conclusion

Rails works magic in so many different areas but the routes are one area that it really shined for me. As overwhelming as this module was, I feel I came away from it with some good lessons.

Top comments (1)

Collapse
 
leastbad profile image
leastbad

Keep up the good work! Rails will give you back 10x what you put into the tank.