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?
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"
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
No, they don't have seperate views. So do you have a app/views/posts/create.html.erb
file?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With