Rails guidelines for RESTful CREATE and UPDATE controller methods

OK, I'm trying to understand the best methods for CREATE and UPDATE methods for HTML and XML formats. The default code for the controller that generates the rail generator is a bit unclear to me.

For the CREATE method, given good preservation, the generator says " redirect_to (@whatever) " for HTML and " render: xml => @whatever ,: status =>: created ,: location => @whatever " for XML.

Given poor persistence, the generator says " render: action => 'new' for HTML and render: xml => @ whatever.errors ,: status =>: unprocessable_entity " for XML.

However, for the UPDATE method, given a good update, the generator says " redirect_to (@whatever) " for HTML and " head: ok " for XML.

And given the poor update, the generator says " render: action => 'edit' " for HTML and render: xml => @ whatever.errors ,: status =>: unprocessable_entity "for XML.

I understand this, and it makes sense to me, and it works fine - BUT, I have two questions:

First, for a successful CREATE and UPDATE, HTML format, why is " redirect_to (@whatever) " instead of " render: action => 'show' "? I understand the differences between redirecting and rendering, just more curious how you guys tend to do this and why. Redirecting would seem like an extra trip for the browser.

Secondly, why is " head: ok " after successful UPDATE via XML, but " render: xml => @whatever ,: status =>: created ,: location => @whatever " after successfully creating CREATE via XML? It seems inconsistent to me. It seems that a successful UPDATE via XML should be the same as a successful CREATE via XML. It looks like you will need a new / updated object so you can test it. How do you do it and why?

+6
rest ruby-on-rails
source share
2 answers

I already wrote this when Sam C answered, but here anyway :-)

For the first part - why redirect instead of rendering? Two reasons I can think of:

1) This is consistent. If you select the show action and the user uses the back button again and then returns to this page, the user will see unexpected behavior. Some versions of IE will give you some IIRC session timeout error, other browsers may handle it a little more elegantly.

The same thing happens if the user closes this page and returns to it later using a GET request - they will not see the show action. Your application may give an error or may have an index action because the user requests a URL, for example http://my.app.com/users , which will be displayed in the index action when using a GET request.

2) if you visualize the show action without redirecting to the GET request and the user is updated, your browser resends the POST request with all the same data, potentially creating duplicate instances of what you created. The browser will warn the user about this so that they can interrupt, but this is potentially confusing and unnecessary inconvenience.

Regarding the second part of your question, not too sure to be honest. I assume that since you are already updating the object in question, you already have a copy of it, so there is no need to return another instance. Having said that updating an object can trigger various callbacks that change other attributes of the object, so returning this updated object with these changes might make sense.

+1
source share

Configure or update redirect_to(@whatever) to clear the message so that the user does not resubmit the updates. It also shows the correct URL in the address bar for the creation case, which fits in the collection path (/ whatevers).

head :ok makes a minimal response to an update, when usually you already have an object in dom. If you refresh the page after the refresh, the standard method is to use rjs views to refresh the dom elements and render the part.

+1
source share

All Articles