Rails refreshes the page after a render call on validation failure

So, I have a controller action similar to this:

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

When the check fails, a new view of the actions is displayed, and the user can see the errors that were made. The problem is that this also changes the URL to localhost:3000/terms, so when for some reason the user wants to refresh the page, then the rails will want to redirect that user to the pages index. Is there an easy way to keep the user on the page new termafter the update?

In fact, I do not have a page indexfor terms, because I do not need this, so this whole situation will cause an error in this case.

+4
source share
3

, :

terms, " ". , https, "" . "get" "post". , , .

, "post" .

post "new-term" => terms#new

, , :

render new_term_path
+2

, render :new create, new, (.. new.html.erb) URL- URL- create ( /terms).

/terms/new, redirect_to new_term_path, , .

, index - ,

def index
  redirect_to new_term_path
end

, , . , .

+2

. resources route.rb resource. , :

resources :posts, :only => [:index, :create, :show]

, routes.rb:

resources :posts

, .

0

Source: https://habr.com/ru/post/1649330/


All Articles