1

I have six distinct sections of my Rails application, all of which have their own models, views, and controllers.

I'm trying to create a "dashboard" page that accesses variables from each of the sections. For instance, in one of my controllers, I have this condition:

if @retirementsavingsdiff < 0
  @retiregrade = "pass"
end

I can't seem to access this variable from a different view/controller though.

Do I put my dashboard logic in application_controller.rb?

4
  • are these variables defined inside the controllers. cant they be moved to helpers files? Commented May 17, 2014 at 3:07
  • Yes, they're all in controllers. Are helpers accessible across the app? Commented May 17, 2014 at 3:09
  • You could create a DashboardController and in the index method set all your needed variables. Commented May 17, 2014 at 3:11
  • So I should duplicate my logic in all my other controllers in DashboardController? Commented May 17, 2014 at 3:12

3 Answers 3

3

A good option for making code reusable is separating it out into modules. Rails 4 includes something called Concerns that make this really easy. Here's a blog post with a good illustration of using Concerns for Controllers, and here's a sample of what your code might look like:

# /app/controllers/concerns/retirement_grade_checker.rb
module RetirementGradeChecker
  extend ActiveSupport::Concern

  def check_retire_grade
    @retirementsavingsdiff = params[:retirementsavingsdiff]
    if @retirementsavingsdiff < 0
      @retiregrade = "pass"
    end
  end
end

# /app/controllers/retirement_controller.rb
class RetirementController < ApplicationController
  include RetirementGradeChecker

  def index
    check_retire_grade
    #... other stuff
  end
end

# /app/controllers/dashboard_controller.rb
class DashboardController < ApplicationController
  include RetirementGradeChecker

  def index
    check_retire_grade
    #... other stuff
  end
end
Sign up to request clarification or add additional context in comments.

7 Comments

How do I know if the new module I created is accessible to other controllers? I just tried this design and got uninitialized constant FinancesController:: RetirementGradeChecker
Did you stick your module in app/controllers/concerns?
Yes. Does it matter what I save it as?
It's the "include RetirementGradeChecker" that is throwing off the error
It's like any other class or module in Rails. The file name needs to match the class name in order for auto loading to work.
|
1

I would avoid using view helpers and instead create a new class or module with all of your logic inside. By doing that you can reuse that logic whenever you need it.

Why do this instead of helpers? You can easily test it.

2 Comments

So move all my logic, across all six controllers, into one new helper that's shared across all my other models?
Check out @James Mason's answer. He goes into more details.
0

methods defined inside helpers are automatically available across all views.

if you want to convert a method defined inside the controller to a helper method, you can do that too:

def my_method
  # code
end

 helper_method :my_method

UPDATE:

here is an example from API

class ApplicationController < ActionController::Base
  helper_method :current_user, :logged_in?

  def current_user
    @current_user ||= User.find_by(id: session[:user])
  end

  def logged_in?
    current_user != nil
  end
end

3 Comments

When I add the helper_method into my controller, I get this error: syntax error, unexpected ':', expecting keyword_end helper_method:
sorry, it was a typo. see the updated answer. it should be helper_method :my_method
I think @James Mason has a better solution to your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.