Answer the question
In order to leave comments, you need to log in
Why does the content of the page change when rendering in another view, but the url is only different in Rails 4???
Rails version 4. REST controller.
Normal addition via /product/new
And in the Product controller there is a create method:
# POST /product
def create
@product = Product.new(params[:product])
if @product.save
redirect_to @product
else
render :new
end
end
Answer the question
In order to leave comments, you need to log in
Actually everything is ok. There is nothing terrible in this and everything is logical.
In flash, you don't need to add anything. It was designed for short text messages, not objects. Because it is saved in the session, and there is not much to clear up. Plus with serialization you wave.
If you really really want to change the address, then hack like this: add this to the new.html.erb template
:
<% unless @product.valid? %>
<script>window.history.replaceState(null,null,"products/new")</script>
<% end %>
And why should it be otherwise?
Logic:
When a POST request is made to the address /product
If the product is saved, then go to the product page
Otherwise, display the new view
So it turns out that if the save fails, you get the new view at the same address.
You can redirect to /product/new in the same way as to product, just don’t forget to save the parameters in flash and restore them to the form there - no one wants to fill out forms 10 times because of an error.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question