2

I have a rails class Datatable that returns an as_json object.

I want to then take that object and assign it to several instance vars to update partials, so when a user calls a myriad of ajax options, I can do one json object server-side, assign them to vars server-side, and reload by partial erb.

How can I call Model.refresh_objects to use in my controller before I call my update_divs.js.erb?

If I'm off-base on how this should be done, please teach me.

1 Answer 1

1

If i understand you correctly,you need after_action methods to use.

http://apidock.com/rails/v4.0.2/AbstractController/Callbacks/ClassMethods/after_action

UPD

BaseController < ApplicationController
  before_action :some_action_name, only: [:show, :edit, :update, :destroy]

  def some_action_name
  end
end

HomeController < BaseController
  def show
  end
end

In this case BaseController is inherited by ApplicationController, we placed some_action_name method to BaseController and inherited HomeController from BaseController. We defined that some_action_name must ne executed before actions show, edit, update, destroy. It means that now some_action_name will be executed before these methods of HomeController too.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I suppose this is the best action to take. I would put my method in the application controller so I can use on both my home controller and x controller?
Yes, you can place it either in application controller(if you have only 2 controllers), or you can create parent controller for your both controllers (if you have other controllers that musn't inherit this before_action)
Interesting...which would be best? By parent controller, do you mean super controller? Can you give an example in your answer of how to apply inheritance?

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.