Rails has taught me some really good best practices. Let’s look at a default rails create action
1 2 3 4 5 6 7 8 9 10 |
|
I try not to deviate too much from this and nor should you. If @post.save
returns true. return a 201 status code. If @post.save
returns false re-render the :new action. So you can let the user fix the form.
Often I will see things like this.
1 2 3 4 5 6 7 8 |
|
At first glance this looks clean. But if you look closer. There is a subtle problem here. If the post object fails validation and fails to create, The action redirects to the index action where all new instaces are creating. The @post object has lost any knowlege of the params and it will not get to oportunity to display the errors from the object. You’ll have to resort to things like flash[:notice] = "@post.errors.full_messages.join()"
… ugh.
Active record gives us a nice interface for displaying errors on an object.
1 2 3 |
|
1 2 3 4 5 6 7 8 |
|
When you call @post.save
OR @post.valid?
errors are added to the @post object so they can be displayed within the form.
Digging even deeper… Check out ActiveModel::Model. It will give you this same object interface without needing database backed model. Powerful stuff.