Filters

Filters are methods that are run before or after a controller action.

Example before_action

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :delete]

  # ...

private
  def set_post
    @post = Post.find(params[:id])
  end

end

Example after_action

class PostsController < ApplicationController
  after_action :format_post, only: [:show]

  # ...

private
  def format_post
    @post.title.upcase
  end
end

Pro tip: Use the <- and -> arrow keys to move back and forward.

Edit this page

See a typo or an error? You can improve this page. This website is available on GitHub and contributions are encouraged and welcomed. We love pull requests from you!