Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails refreshing page after calling render on failed validation

So I have a controller action looking like this:

def create
  @term = Term.new(term_params)
  if @term.save
    redirect_to(@term)
  else
    render :new
  end
end

When validation fails the new actions view is rendered and user can see the errors that were made. The problem is that this also changes the URL to localhost:3000/terms so when the user would for some reason want to refresh the page, then rails would want to redirect that user to terms index page. Is there a simple way to keep the user on the new term page after refresh?

I actually don't have a index page for terms as I dont't need it, so this whole situation will throw an error in this case.

like image 524
Kaspar Avatar asked Jul 27 '16 19:07

Kaspar


1 Answers

Please,take this in consideration:

Your are seeing terms but is not the same like the "index of terms". Thing is, using https, in the path of your "new" action. Index use "get" and create use "post". So, looks the same but is not.

I think in your routes files, you need to define the "post" path in a explict way.

post "new-term" => terms#new

and maybe define in your controller to:

render new_term_path
like image 186
Carlos Orellana Avatar answered Oct 12 '22 02:10

Carlos Orellana