hlee
hlee

Reputation: 339

Logic for Application Layout

I am trying to make an ActiveRecord call to get information for the application layout (the default application.html.haml layout). I know I am not supposed to put logic into the layout itself, but I am unsure of where to put it.

The line of code I need run in the layout is just a simple Model call:

Client.find_by(:id => current_user.client_id)

Upvotes: 1

Views: 125

Answers (2)

Dan
Dan

Reputation: 1267

It's okay if you put it in ApplicationController. And you can put controller related code to controllers/concerns folder.

'concerns/concern.rb':

module Concern
  def method
    # Your code here
  end
end

To use a module from concerns folder include it in the controller: include Concern

Upvotes: 0

user419017
user419017

Reputation:

I would suggest throwing it in helpers/application_helper.rb. I've used this in the past for things such as title helpers and body class helpers.

# helpers/application_helper.rb
module ApplicationHelper
  def body_class
    [controller_name, action_name].join(' ')
  end
end

# views/layouts/application.slim
body class=body_class
  = yield

The ApplicationController isn't for such helpers. It's mainly as support for your controllers, not your views.

Upvotes: 1

Related Questions