Rails 3:使用部分Ajax更新视图

时间:2011-10-14 19:52:58

标签: ajax ruby-on-rails-3.1 partial-views

我在下面的Ajax请求中做错了什么......?

链接/电话:

<%= link_to 'Business Analysis', it_business_analysis_path, :remote => true %>

路线:

match 'it/business_analysis' => 'informationtechnology#business_analysis', :as => :it_business_analysis

控制器:

class InformationtechnologyController < ApplicationController  
  def business_analysis

    render :update do |page|
      page.replace_html 'page_content', :partial => 'business_analysis'
    end

  end
end

视图:

<div id="page_content">
</div>

部分称为:

_business_analysis.html.erb

错误:

Action Controller: Exception caught

<h1>Template is missing</h1>
<p>Missing template informationtechnology/update, application/update with {:formats=&gt;[:js, &quot;application/ecmascript&quot;, &quot;application/x-ecmascript&quot;, :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :handlers=&gt;[:builder, :coffee, :erb], :locale=&gt;[:en, :en]}. Searched in: * &quot;/home/<user_name>/Websites/<project_name>/app/views&quot;

似乎Rails正在寻找一个名为“更新”的视图 - 为什么以及如何解决这个问题?

非常感谢! 汤姆

1 个答案:

答案 0 :(得分:1)

好的,对于任何有类似问题的人,我找到了解决方案:

问题在于,在Rails3中,Prototype被jQuery取代。因此,以下代码不再有效:

render :update do |page|
  page.replace_html 'page_content', :partial => 'business_analysis'
end

以下2个链接将解释有关如何处理jQuery的详细信息:

Rails 3: Simple AJAXy Page updates?

http://railscasts.com/episodes/205-unobtrusive-javascript

汤姆