使用Ruby on Rails预加载布局

时间:2022-07-09 20:42:11

My application has to do a rather time-consuming operation only on the first 'show' action of the page. I have a certain layout in app/views/layouts/application.html.erb that I would like to show from the very first second, so that a spinner alongside with an informative message would be inside this layout, letting the user know that the operation will take some time to finish. After this operation is finished, I would like to substitute this message by the real content, if possible changing this via AJAX, but I would not mind to just refresh the page for the time being.

我的应用程序只需要在页面的第一个“show”操作上执行相当耗时的操作。我在app / views / layouts / application.html.erb中有一个特定的布局,我想从第一秒开始显示,这样一个微调器旁边有一条信息性的消息将在这个布局中,让用户知道操作需要一些时间才能完成。完成此操作后,我想用真实内容替换此消息,如果可能通过AJAX更改此消息,但我不介意暂时刷新页面。

I have this in my show action, the costly process is 'load_suggestions' (around 5sec).

我在演出动作中有这个,昂贵的过程是'load_suggestions'(大约5秒)。

respond_to do |format|
      format.html do
        if @issue.suggestions == [] 
          load_suggestions
        end
      end
      format.xml  { render :xml => @issue }
      format.js   
      end 

I believe that this post - Show loading screen while performing task in Rails 3 - has some rather interesting information for what I try to accomplish. Basically I would include this javascript function in views/issues/show.js.erb. Problem is that if I include it there, it would get called every single time, and that is not what I intend it to do.

我相信在Rails 3中执行任务时这个后显示加载屏幕 - 有一些相当有趣的信息,我试图完成。基本上我会在views / issues / show.js.erb中包含这个javascript函数。问题是如果我把它包含在那里,它会每次被调用,这不是我打算做的。

Any suggestions, clues, guidance?

有什么建议,线索,指导吗?

1 个解决方案

#1


0  

you could try out the new rails streaming api and render some waiting javascript code before the rest of your stuff get's loaded.

您可以尝试新的rails流式传输api并在其余的东西被加载之前呈现一些等待的javascript代码。

i would not consider your approach a good idea. instead i would try to put the slow stuff in the background and use ajax to retrieve it after it finished.

我不认为你的方法是个好主意。相反,我会尝试将缓慢的东西放在后台,并在完成后使用ajax检索它。

another approach would be to do some "warmup" where you load the stuff before your application is launched. in an initializer for example.

另一种方法是做一些“预热”,在你启动应用程序之前加载这些东西。例如,在初始化程序中。

#1


0  

you could try out the new rails streaming api and render some waiting javascript code before the rest of your stuff get's loaded.

您可以尝试新的rails流式传输api并在其余的东西被加载之前呈现一些等待的javascript代码。

i would not consider your approach a good idea. instead i would try to put the slow stuff in the background and use ajax to retrieve it after it finished.

我不认为你的方法是个好主意。相反,我会尝试将缓慢的东西放在后台,并在完成后使用ajax检索它。

another approach would be to do some "warmup" where you load the stuff before your application is launched. in an initializer for example.

另一种方法是做一些“预热”,在你启动应用程序之前加载这些东西。例如,在初始化程序中。