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

Example skip_before_action

class ApplicationController < Jets::Controller::Base
  before_action :authenticate_user_session

  # ...


class PublicDocumentsController < ApplicationController
  # skip the authenticate_user_session for all the methods in the controller
  skip_before_action :authenticate_user_session

  # ...

class PostsController < ApplicationController
  # skip the authenticate_user_session only for the index method in the controller
  skip_before_action :authenticate_user_session, only: [:index]


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!