8

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.

1
  • If you don't want to change the url on validation fail. Handle create action through ajax. As If you will render it will show create url only. Jul 27, 2016 at 19:51

3 Answers 3

3

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
2
  • Feels a bit hackish :/
    – inimene
    Jul 27, 2016 at 20:39
  • In short terms: Index action, use Https "get" method. This method will get all data. Create action use Https "post" method. Is important you notice the difference. You may see the same in the url, but is not. Then, you just need to implement the method that I mentioned above. I think you can do even something like: "redirect_to new_term_path" and don't pass the @term object because is not created. I think is you use this: redirect_to new_term_path you won't see the error message again when refresh the page, becase the call is now redircting the full_path and not just the template Jul 27, 2016 at 20:52
2

It's important to realize that when you do render :new inside your create action, you're not actually routing to and running the new action, you're simply rendering the new template (i.e. new.html.erb) so the URL reflects the URL of the create action (in your case /terms).

If you wanted to actually route your users to /terms/new, you can redirect_to new_term_path if the save fails although that would also prevent your users from seeing the validation errors.

Since you mentioned that you are not actually using the index action to render anything meaningful, have it perform the redirect

def index
  redirect_to new_term_path
end

This way if the user happens to refresh the page, they'll be redirected back to the new form. This isn't a general solution but should work well in your specific case.

0

Actually there is a fix. Make the resources in your route.rb file as resource. This will not create the index action unless you explicity ask for it like:

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

otherwise it will not generate the index action if you do this in routes.rb file:

resources :posts

I hope this helps.

5
  • I do need the id tough. This would lose the params.
    – inimene
    Jul 27, 2016 at 19:57
  • No it will not. The redirection will have the params placed in the fields
    – Jun Aid
    Jul 27, 2016 at 19:59
  • Now it will want to render the show actions view. That's not much better also :D
    – inimene
    Jul 27, 2016 at 20:10
  • thats strange. Which url it renders ?
    – Jun Aid
    Jul 27, 2016 at 20:18
  • /term. And according to documentation this will lead to show.
    – inimene
    Jul 27, 2016 at 20:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.