W
W
WEB_champion2014-11-03 12:56:10
Ruby on Rails
WEB_champion, 2014-11-03 12:56:10

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

Let’s say it didn’t work to add a product, and :new should be rendered, and it works, the page content changes, but instead of the page url changing to /product/new, it changes to /product. It's also not pretty.
How to solve the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2014-11-03
@WEB_champion

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 %>

_
_ _, 2014-11-03
@AMar4enko

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 question

Ask a Question

731 491 924 answers to any question