Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

render :new not going to the right place after a validation

I have a 'new' form that gets validated in a post model. When the validator kicks in, it renders incorrectly.

The new post page path is at '/posts/new'

On validation, the new post page path is at '/posts' .. I need it to go back to '/posts/new'.

This is my controller:

def create
 @post = current_user.posts.build(params[:post])
 if @post.save
   redirect_to public_post_page_path(@post.public_url)
 else 
   render :action => :new
 end
end

I have a feeling it might have to do with my form. So here is the formtastic first line:

<%= semantic_form_for [:student, post], :html => {:id => "post_form"} do |form| %>
like image 673
Marc Avatar asked Jan 08 '12 05:01

Marc


1 Answers

This is the correct behavior from rails.

In the create action it simply renders the "new" view file. As such the url will be /posts but the view will correctly display the form. There is nothing wrong with this behavior; and in general rails convention is good form. Also the built in rails errors work if you just render new; however if you redirect they won't display.

If you really feel like you want to go back to that url you need to use:

redirect_to 

instead of render.

like image 167
Msencenb Avatar answered Nov 17 '22 19:11

Msencenb