0

我要做的是根据在 locality_type 选择中选择的选项更改可供选择的地区使用的列表。两个选择都在设施/_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'
4

2 回答 2

2

Prototype 和 RJS 已从 Rails 3.1 中删除到单独的 gemprototype-rails中。确保将其包含在 Gemfile 中。

于 2011-08-17T17:24:20.057 回答
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;
}
于 2011-08-19T07:08:01.427 回答