【发布时间】:2010-07-23 15:05:32
【问题描述】:
刚刚尝试在我的模型中编写一个简单的 validates_presence_of,当错误尝试呈现时,它会调用:
Template is missing
Missing template posts/create with {:locale=>[:en, :en], :handlers=>[:builder, :rjs, :erb, :rhtml, :rxml, :haml], :formats=>[:html]} in view paths "/Users/johnsmith/Sites/shwagr/app/views"
Rails3 中的错误没有单独的视图,是吗?我认为那是 Rails 的魔法..
想知道是否有人遇到过这个问题,或者知道如何正确验证。
我的模特:
validates_presence_of :category, :name, :url
我的控制器:
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
def create
@post = Post.new(params[:post])
if @post.valid? && current_user.posts << @post
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
end
更新
有趣的是,我“触摸 app/views/posts/create.html.haml”,现在它删除了错误并改为加载该页面。但为什么会呢?或者更重要的是,我怎样才能让它像它应该的那样重定向回 new_post_path(@post)?
【问题讨论】:
标签: ruby-on-rails ruby validation ruby-on-rails-3