Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Ruby on Rails - Controller Basics

Introduction

Controllers in Rails are the central part of the MVC (Model-View-Controller) architecture. They are responsible for handling incoming web requests, interacting with models, and rendering views. This guide will introduce you to the basics of controllers in Rails.

Key Points:

  • Controllers handle incoming requests and provide responses.
  • They interact with models and render views.
  • This guide covers the basics of creating and using controllers in Rails.

Creating a Controller

To create a new controller, you can use the Rails generator command:

# Generate a new controller named Articles with index, show, new, and edit actions
rails generate controller Articles index show new edit
                

This command creates the controller file, view templates for each action, and a route for the controller.

Controller Actions

Controller actions are methods that handle specific requests. Here is an example of a simple controller with actions:

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end

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

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)
    if @article.save
      redirect_to @article
    else
      render :new
    end
  end

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

  def update
    @article = Article.find(params[:id])
    if @article.update(article_params)
      redirect_to @article
    else
      render :edit
    end
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to articles_path
  end

  private

  def article_params
    params.require(:article).permit(:title, :body)
  end
end
                

This controller provides actions for listing, showing, creating, editing, updating, and deleting articles.

Strong Parameters

Strong parameters are used to prevent mass assignment vulnerabilities by specifying which parameters are allowed. Here is an example:

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  # Other actions...

  private

  def article_params
    params.require(:article).permit(:title, :body)
  end
end
                

In this example, only the title and body parameters are permitted for mass assignment.

Rendering Views

Controllers use the render method to display views. By default, Rails automatically renders the view that corresponds to the action name. Here is an example:

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
    # Automatically renders app/views/articles/index.html.erb
  end

  def show
    @article = Article.find(params[:id])
    # Automatically renders app/views/articles/show.html.erb
  end
end
                

Redirecting Requests

Controllers use the redirect_to method to redirect requests. Here is an example:

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def create
    @article = Article.new(article_params)
    if @article.save
      redirect_to @article
    else
      render :new
    end
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to articles_path
  end
end
                

In this example, the create action redirects to the article's show page upon successful save, and the destroy action redirects to the articles index page after deletion.

Filters

Filters are methods that run before, after, or around controller actions. Here is an example using a before filter:

# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  before_action :find_article, only: [:show, :edit, :update, :destroy]

  def show
    # @article is already set by the before_action
  end

  def edit
    # @article is already set by the before_action
  end

  def update
    if @article.update(article_params)
      redirect_to @article
    else
      render :edit
    end
  end

  def destroy
    @article.destroy
    redirect_to articles_path
  end

  private

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

  def article_params
    params.require(:article).permit(:title, :body)
  end
end
                

In this example, the find_article method is called before the show, edit, update, and destroy actions.

Conclusion

Controllers are a fundamental part of the Rails MVC architecture, handling incoming requests, interacting with models, and rendering views. By understanding the basics of controllers, you can effectively manage the flow of your Rails application and ensure a clean separation of concerns.