我的应用程序中有一个名为 _dashboard.html.erb 的部分页面。在仪表板内部,我有很多调用来渲染不同的部分。其中之一是 _calendar.html.erb 部分,它调用谷歌日历并下拉事件列表。此加载需要时间,我希望在仪表板的其余部分加载后部分加载 _calendar。需要重申的是,我不希望它基于点击事件,而是基于页面加载时。
页面 Controller .rb
# Logged in to shift
def jump
@title = "#{User.find(session[:user_id]).firstname}'s Dashboard"
@tasks = Task.where(:completed => 0)
@questions = Question.where(:answered => 0)
serv = GCal4Ruby::Service.new
serv.authenticate("email","pass")
cal = GCal4Ruby::Calendar.find(serv, {:title => "Skidmore Center for Sex and Gender Relations"})
#@events = GCal4Ruby::Event.find(serv, {'start-min' => Time.now.utc.xmlschema, 'start-max' => 5.days.from_now.utc.xmlschema })
@events = GCal4Ruby::Event.find(serv, {'start-min' => Time.parse("01/01/2011").utc.xmlschema, 'start-max' => Time.parse("06/01/2011").utc.xmlschema, :calendar => cal.id})
end
跳转是我有仪表板部分渲染的页面。这是/app/views/layouts/_dashboard.html.erb 调用日历部分的部分:
<div id="main-content">
<div class="post">
<h3>calendar</h3>
<%= render 'layouts/calendar' %>
</div>
</div>
和/app/views/layouts/_calendar.html.erb
<% @events.each do |event| %>
<% if event.start_time >= Time.now and event.start_time <= 5.days.from_now %><li><%= event.start_time.to_s(:short) %> <%= event.title %></li><% end %>
<% end %>
我认为您在这里应该牢记的是,虽然我了解 AJAX 的工作原理,但我对它本身或在 Rails 中编码几乎没有经验。在这里提供任何帮助将不胜感激(而且越简单越解释越好!)
谢谢
更新: 我使用了 async_partial gem,但现在在从谷歌日历中部分提取事件时遇到了问题。 partial 在没有 async_partial 的情况下工作。
最佳答案
在没有 gem 的情况下处理这个问题的方法之一是对页面加载事件使用某种事件处理程序,这很常见。例如,使用 jQuery,您可以:
// this get executed when jQuery is made available, which means when
// your document is ready.
$(function () {
// you replace the actual calendar content with the piece that is
// rendered with your partial
$("#your-calendar-div-id").replaceWith('<%= escape_javascript(render 'layouts/calendar') %>');
});
关于ruby-on-rails - Rails 在页面加载后呈现单个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7073075/