Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing templates from new Rails 3 app?

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?

like image 753
Trip Avatar asked Jul 23 '10 15:07

Trip


3 Answers

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"
like image 140
Marcel Jackwerth Avatar answered Oct 08 '22 01:10

Marcel Jackwerth


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
like image 26
Trip Avatar answered Oct 08 '22 01:10

Trip


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

like image 37
John Topley Avatar answered Oct 08 '22 00:10

John Topley