Search code examples
ruby-on-railsbefore-filter

rails the best way to saving page duration and page loading speed


Hi I'm a beginner of rails and I'm not good at English. so if there is some total nonsense please understand..

I'm trying to record loading speed and page duration in every pages. I made a database "pages" and method "savepage" in my "Page" model. To save in every page I put "savepage" method in application controller.

Page.rb

def self.savepage
    .
    .
    .
end

application_controller.rb

before_filter :dosave
def dosave
    Page.savepage
end

these kind of format..

My question is 1. am I doing correct? using before_filter to do save in very first of loading process? 2. to save after loading all the contents in a page what should I use? 3. to save after user leave this page what should I use?

I saw before_destroy and after_filter, but I can't find what it is... what filter means.... what action means destroy.... thank you in advance!


Solution

  • before_filter is the first thing which loads before giving request to controller.But your need is completely different . Fundamentally filter are used boolean checking.If certain method is true,it will run otherwise it may not. This filter are further extended and we put code into that filters.(And Even sometimes it is consider as best practice) .

    Now, before_filter :dosave might be right but is it not true way of knowing page(UI) loading process. I suggest you to use javascript call or use some manually created helper methods and place it into view .erb files.

    May be this will interest you
    https://github.com/grosser/record_activities
    Log user activities in ROR

    what action means ?

    Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straightforward as possible.
    Source : http://guides.rubyonrails.org/action_controller_overview.html I highly suggest you to read above documentation. It is very necessary for you and it covers topic which you asked here.` And one more thing,

    what is action destroy ?

    This is simply an action method just like new. Since, rails follow Convention over configuration ( and its developer too) so they put code which do some delete destroy or some destruction. This make thing simple,otherwise more configuration will require which is against rails policy.