Using an object at a global level Rails

0

I have created a resource with its respective model and controller, in which I seek to customize the style of the site, in which I modify the value of the attribute and when changing, change the style of certain elements of the site, for example:

<nav class="header" style="background: <%= @setting.header_color %><% else %>">

However, although it works in the views of the same resource does not work out of it, since I get a nil error, and wanted to help me to use it regardless of the view, perhaps stating something in application controller

    
asked by Hector Hernandez 18.12.2016 в 00:33
source

1 answer

2

Declare it with a :before_action in the ApplicationController, with that it will work in all the views of all the controllers:

# app/controllers/application_controller.rb    
class ApplicationController < ActionController::Base
  before_action :fetch_settings

  def fetch_settings
    @settings = current_user.settings
  end
end
    
answered by 18.12.2016 / 22:49
source