【问题标题】:render :update do |page| tries to render template "update"渲染:更新做|页面|尝试渲染模板“更新”
【发布时间】:2011-08-17 16:22:13
【问题描述】:

我要做的是根据在 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'

【问题讨论】:

  • 你使用的是什么版本的 Rails?
  • rails 3.1.0.rc5 根据 gem 列表

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


【解决方案1】:

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

【讨论】:

  • 谢谢您,它已修复错误,但仍然没有发生替换。发送像 http://localhost:3100/localities?locality_type=city&element_id=facility_address_attributes_locality_id 这样的请求会给我一个请求 Element.update("facility_address_attributes_locality_id", "<option value=\"2\">London</option>\n<option value=\"1\">Paris</option>"); 但页面没有改变
  • 您是否在 application.js 中包含原型? Rails 3.1 默认包含 jquery。
  • 是的,我确实包含 //= require prototype //= require prototype_ujs //= require effects //= require dragdrop //= require controls 在原型导轨自述文件中
【解决方案2】:

好吧,由于 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;
}

【讨论】:

    猜你喜欢
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2014-03-14
    • 2016-12-30
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多