views:

449

answers:

3

Just tried writing a simple validates_presence_of in my model, and when the errors try to render, it calls this :

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"

Errors don't have seperate views in Rails3 do they? I thought that was Rails magic..

Curious if anyone had this problem, or knew how to make it correctly validate.

My Model:

validates_presence_of :category, :name, :url

My Controller:

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

Update

Interesting, I 'touch app/views/posts/create.html.haml', and now it removed the error and instead loads that page instead. But why would it? Or more importantly, how can I make it just redirect back to the new_post_path(@post) like it should?

A: 

No, they don't have seperate views. So do you have a app/views/posts/create.html.erb file?

John Topley
No..I have a _form/edit/index/new/show ..And this came with the form it was in .. = form_for @post do |f| -if @post.errors.any? #errorExplanation %h2= "#{pluralize(@post.errors.count, "error")} prohibited this post from being saved:" %ul - @post.errors.full_messages.each do |msg| %li= msgIt needs something else?
Trip
Something odd, methinks is happening here though. Because validation 'does' work right out of the box in Rails. I updated what my models look like above.
Trip
OK. Please edit your question to show us the controller code.
John Topley
+1  A: 

If your

if @post.valid? && current_user.posts << @post

line returns false, no render() or redirect_to() is called. Rails' default behavior then is to render the view with the same name as your method. That would be create.BUILDER.FORMAT.

Try to remove the line. Use this code instead:

@post = current_user.posts.new(params[:post])
respond_to do |format|
  if @post.save
    ...

Or write an else case with

render :action => "new"
Marcel J.
A: 

Ah got it. This is because it was never valid so it would loop back to itself on 'create', find no template there and error out. The correct way to set up the def create would be this

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 { redirect_to new_user_post_path(:current) }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  else
    render :action => 'new'
  end
end
Trip