CDub
CDub

Reputation: 13354

Rails 5 before_action with method parameter

Is it possible to do something like the following?

class PageController < ApplicationController
  before_action :set_page, <send parameter `with_includes` to `set_page` as boolean based on conditions>

  ...

  private
  
  def set_page(with_includes = true)
    @page = default ? Page.includes(:child) : Page
  end
end

Upvotes: 1

Views: 1498

Answers (1)

Yakov
Yakov

Reputation: 3201

It's possible to call before_action with a lambda function:

 before_action -> { set_page(your_condition_here) }

Or with a block

before_action do
  set_page(your_condition_here)
end

Upvotes: 2

Related Questions