简体   繁体   中英

How to share data with all views in Rails?

As the title said what the possible ways to share data with all views? In Laravel there is a feature that comes with the framework called viewComposers, I don't know if that exists with Rails.

Well, the most simple one is to use application controller

class ApplicationController < ActionController::Base
  before_action :set_somethings

  def set_somethings
    @page_title = 'Acme Corporation'

    # You can also have service classes and call them here
    @settings = MyService.initializeSettings
  end
end

in your views

<%= @page_title %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM