回形针验证

时间:2010-07-09 16:31:42

标签: ruby-on-rails

我想用回形针验证文件的存在。当我尝试提交表单时,它会回滚,但由于某种原因,它会寻找一个不存在的create.erb模板。我也没有收到任何错误信息

class Image < ActiveRecord::Base
 has_attached_file :photo,
:styles => {
:thumb => "100x100#",
:small => "750x750>" } 
validates_attachment_presence :photo


class ImagesController < ApplicationController
before_filter :require_user, :only => [:new, :create]


def new
 @image = Image.new
end



 def create

 @image = Image.new(params[:image])
 @image.user = current_user
  if @image.save
    flash[:notice] = 'image uploaded'
    redirect_to :controller => "images", :action => "index" 
 end
end

def show
@image = Image.find(params[:id])
end

使用gem 2.3.3版

1 个答案:

答案 0 :(得分:1)

在你的create中,你没有处理@ image.save返回false的情况。

def create

  @image = Image.new(params[:image])
  @image.user = current_user
  if @image.save
    flash[:notice] = 'image uploaded'
    redirect_to :controller => "images", :action => "index" 
  else
    render :new # <== You forgot this.
  end
end

如果没有那件作品,它实际上会再次尝试渲染create并失败。