渲染:更新do | page |尝试渲染模板“更新”

时间:2011-08-17 16:22:13

标签: javascript ruby-on-rails render missing-template

我要做的是根据locality_type select中选择的选项更改选择地点的可用列表。两个选择都在facilities / _form.html.erb上呈现。我在LocalitiesController中有以下代码

def index
@localities = Locality.all(:conditions => {:locality_type => params[:locality_type]},
                           :order => 'name')
loc_select_id = params[:element_id]
render (:update) do |page|
  localities_options = options_from_collection_for_select(@localities, 'id', 'name')
  page.replace_html loc_select_id, localities_options
end
end

这个方法是从address.js调用的:

var locTypeElem = $('select#locality_type');
var locElem = $("select[name$='[locality_id]']");
var locQuery = '/localities?locality_type=' + locTypeElem.val()
   + '&element_id=' + locElem.attr('id')
$.get(locQuery, null, null, 'script');

之前我没过,但这次出了什么问题,我在开发日志中收到以下错误消息:

ActionView::MissingTemplate (Missing template localities/update, application/update    with {:formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:ru, :ru], :handlers=>[:builder, :erb]}. Searched in:
 * "D:/Work/Reserv.by/app/views"
 * "D:/Dev_apps/Ruby187/lib/ruby/gems/1.8/gems/kaminari-0.12.4/app/views"
 * "D:/Dev_apps/Ruby187/lib/ruby/gems/1.8/gems/devise-1.4.2/app/views"
 ):
 app/controllers/localities_controller.rb:7:in `index'

2 个答案:

答案 0 :(得分:2)

原型和RJS已从Rails 3.1中移除到单独的gem prototype-rails。确保将其包含在Gemfile中。

答案 1 :(得分:0)

好吧,因为不推荐使用Prototype,我尝试了另一种解决方案: LocalitiesController现在返回数据,而不是脚本:

def index
@localities = Locality.all(:conditions => {:locality_type => params[:locality_type]},
                           :order => 'name')
render :inline => "<%= options_from_collection_for_select(@localities, 'id', 'name') %>"
end

此数据由AJAX处理:

function selectLocalitiesByLocalityType()
{
var locTypeElem = $('select#locality_type');
var locElem = $("select[name$='[locality_id]']");
var locQuery = '/localities?locality_type=' + locTypeElem.val()
$.ajax({
    url: locQuery,
    method: 'GET',
    dataType: 'html',
    success: function(data) {
        locElem.empty();
        locElem.append(data);},
    error: function(data) {alert(data);}
        });
return false;
}