开发者

paperclip validations

I would like to validate the presence of a file with paperclip. When I attempt to submit the form it rolls back, but for some reason it goes looking for a create.erb template which does not exist. I don't get any sort of error message either

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

using th开发者_开发知识库e gem, version 2.3.3


In your create you are not handling the case where @image.save returns 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

Without that piece it's actually trying to render create again, and fails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜