2

我正在尝试按照本指南在 Rails 3.2 应用程序上设置 jquery 文件上传。

一切都几乎完美无缺,但是,当我点击“开始上传”时,我在 Chrome 的控制台中看到一个错误:

POST http://testapp.dev/photos 500 (Internal Server Error) 

在我得到的日志中:

ActionView::MissingTemplate (Missing template photos/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}.

我已经通过带有细齿梳的控制器,但我无法弄清楚是什么导致了这个错误,或者它为什么要寻找一个创建部分。什么是系统的调试方法?

谢谢

编辑

我的控制器操作如下所示:

class PhotosController < ApplicationController

  def index

   @photos = Photo.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @photos }
      format.json { render :json => @photos.collect { |a| a.to_jq_upload }.to_json }
      format.js { render :json => @photos.collect { |a| a.to_jq_upload }.to_json }
    end
  end

  def show
    @photo = Photo.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @photo }
      format.json { render :json => @photo }
      format.js   
    end
  end

  def new
    @photo = Photo.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @photo }
      format.json { render :json => @photo }
      format.js 
    end
  end


  def edit
    @photo = Photo.find(params[:id])

  end

  def create
    @photo = Photo.new(params[:photo])

    respond_to do |format|
      if @photo.save
        format.html {  
                  render :json => [@photo.to_jq_upload].to_json, 
                  :content_type => 'text/html',
                  :layout => false
                }
        format.json { render :json => [ @photo.to_jq_upload].to_json }
        format.js

      else 
        format.html { render :action => "new" }
        format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
        format.json { render :json => [  {:error => "An error was encountered while processing your photos. Please try again."}], status: 304 }
        format.js
      end      
    end
  end



      def update
    @photo = Photo.find(params[:id])

    respond_to do |format|
      if @photo.update_attributes(params[:photo])
        format.html { redirect_to(@photo, :notice => 'Asset was successfully updated.') }
        format.xml  { head :ok }
        format.json { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /assets/1
  # DELETE /assets/1.xml
  def destroy
    @photo = Photo.find(params[:id])
    @photo.destroy

    respond_to do |format|
      format.html { redirect_to(photos_url) }
      format.xml  { head :ok }
      format.json { render :json => true }
      format.js
    end
  end
    end
4

1 回答 1

7

看起来该respond_to块正在format.js尝试渲染模板(如果没有提供选项,则为默认值)。

因为您使用的 jQuery-file-upload 插件需要特定的 JSON 响应,所以respond_to似乎没有必要使用块,您可以使用以下内容

def create
    @photo = Photo.new(params[:photo])

    if @photo.save
        render :json => [ @photo.to_jq_upload].to_json
    else 
        render :json => [{ :error => "An error was encountered while processing your photos. Please try again." }], :status => 304
    end      
end

我还强烈推荐调试器gem,它允许您在应用程序中设置断点,以便您更好地了解正在发生的事情。

要添加debuggergem,您需要先将 gem 添加到您的Gemfile并从命令行运行bundle installdebugger接下来,您只需在要设置断点的代码行上添加单词。在你的情况下,你可以做

def create
    @photo = Photo.new(params[:photo])
    debugger

    if @photo.save
    # ... rest of code

最后,您需要使用该--debugger选项重新启动服务器。当 jQuery-file-upload 插件将请求发布到服务器时,它会遇到断点,您可以更好地分析传递的参数以及单步执行代码以了解正在发生的事情。干杯。

于 2012-06-12T04:32:13.627 回答