0

嗨,我有以下表格:

<%= form_for featured_content, :remote => true do |f| %>
                                <%= f.text_field :url, :value => featured_content.url, :class => "form_text_field ajaxSingle" %>
                                <%= button_tag "Update", :class=>'greystyle', :type=>"submit" %>
                            <% end %>

使用以下 JS:

    $('.ajaxSingle').live('keyup', function() {
  $(this).parents('form:first').submit();
});

这将转到以下控制器操作:

  def update
    @fc = FeaturedContent.find(params[:id])
    @fc.update_attributes(params[:featured_content])
  end

这一切都完美无缺。但是,每次更新字段时,我都会在 javascript 控制台中收到以下错误:

ActionView::MissingTemplate (Missing template featured_contents/update, application/update with {:locale=>[:en], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:builder, :erb, :coffee]}. Searched in:
  * "/Users/rordev/Code/RubyOnRails/Projects/EverythingEver/Dev/everythingever/app/views"

我只有创建、销毁、更新和索引。我该如何摆脱这个错误?

4

1 回答 1

2

你必须告诉 Rails 为你的 ajax 调用提供一个专门的响应:

def update
  @fc = FeaturedContent.find(params[:id])
  @fc.update_attributes(params[:featured_content])
  respond_to do |format|
    format.html
    format.js   { render :json => {:status => 'ok' } } #or any response you might find usefull
  end
end
于 2012-12-31T15:42:10.640 回答