Is the “Rails Way” for “updating” fundamentally wrong?

I deliberately ask this question in an inflammatory way, because it bothers me that I'm missing something.

The Rails Way for working with updating the model is as follows:

class UsersController < ApplicationController ... def update @current_user = load_user if @current_user.update_attributes params[:user] redirect_to success_path else render :edit end end end 

This is all good and good, except that you get to an odd url when the form is misrepresented:

User Editing

You get on the way:

 users/:user_id/edit 

After submitting changes that are not checked

i.e. you will need to correct the entries in the form and resubmit:

 users/:user_id 

After making changes that confirm

 success_path 

Why the hell should you be on a different url just because the form has errors?


Problem...

You do the same, but now you have a different url. It's a bit strange.

In fact, frankly this seems wrong. You are on a form that has not been validated correctly and therefore rebooted. You should still be at /users/:user_id/edit . If you did a JS check, you would do it.

Also, if you have some kind of “currently selected” logic in your navigation, you are actually not visually in the place where the correct navigation element is no longer highlighted - it looks like you are in the user’s profile page

+4
source share
2 answers

Why the hell should you be on a different url just because the form has errors?

Because when you first went:

 users/:user_id/edit 

... you requested a GET.

Then you sent a message:

 users/:user_id 

Thus, by sending a form message, you requested a different resource route and, by definition, specified a different URL.

The framework doesn’t care what happened in the background while processing your request - all it knows is POST (which by convention is not necessarily idempotent as GET)

+6
source

This is actually not a "Rails Way", it is a "REST Way". Wikipedia: Submission View State

If you follow the rules, you get a free REST web service. As I understand it, the path "resource / id / edit" is specific to html documents. Web service clients do not need an edit form.

So the guys tried to be consistent. If you do not need compatibility with web services, you can, of course, change the routes.

0
source

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


All Articles