mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 16:18:01 +00:00
Compare commits
2 Commits
117856c2aa
...
a92b6a3848
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a92b6a3848 | ||
|
|
e7457a9e54 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1260 TILs and counting..._
|
||||
_1261 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -394,7 +394,6 @@ _1260 TILs and counting..._
|
||||
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
|
||||
- [Check Classes On A DOM Element](javascript/check-classes-on-a-dom-element.md)
|
||||
- [Check If A Number Is Positive Or Negative](javascript/check-if-a-number-is-positive-or-negative.md)
|
||||
- [Check If An Object Is Empty With Zod](javascript/check-if-an-object-is-empty-with-zod.md)
|
||||
- [Check If Something Is An Array](javascript/check-if-something-is-an-array.md)
|
||||
- [Check The Password Confirmation With Yup](javascript/check-the-password-confirmation-with-yup.md)
|
||||
- [Compare The Equality Of Two Date Objects](javascript/compare-the-equality-of-two-date-objects.md)
|
||||
@@ -857,6 +856,7 @@ _1260 TILs and counting..._
|
||||
- [Use IRB And Ruby Flags With Rails Console](rails/use-irb-and-ruby-flags-with-rails-console.md)
|
||||
- [Verify And Read A Signed Cookie Value](rails/verify-and-read-a-signed-cookie-value.md)
|
||||
- [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md)
|
||||
- [Why Redirect And Return In Controllers](rails/why-redirect-and-return-in-controllers.md)
|
||||
- [Wipe Out All Precompiled Assets](rails/wipe-out-all-precompiled-assets.md)
|
||||
- [Write Reversible Migration To Set Default](rails/write-reversible-migration-to-set-default.md)
|
||||
- [Write Safer Where Clauses With Placeholders](rails/write-safer-where-clauses-with-placeholders.md)
|
||||
@@ -1499,6 +1499,7 @@ _1260 TILs and counting..._
|
||||
|
||||
### Zod
|
||||
|
||||
- [Check If An Object Is Empty With Zod](zod/check-if-an-object-is-empty-with-zod.md)
|
||||
- [Create Union Type Of Nearly Identical Objects](zod/create-union-type-of-nearly-identical-objects.md)
|
||||
|
||||
## Usage
|
||||
|
||||
38
rails/why-redirect-and-return-in-controllers.md
Normal file
38
rails/why-redirect-and-return-in-controllers.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Why Redirect And Return In Controllers
|
||||
|
||||
A fairly common idiom in Rails controller actions is a `redirect_to` followed
|
||||
by an `and return`.
|
||||
|
||||
```ruby
|
||||
def show
|
||||
redirect_to sign_in_path and return if current_user.blank?
|
||||
|
||||
book = Book.find(params[:id])
|
||||
|
||||
render book
|
||||
end
|
||||
```
|
||||
|
||||
Because a `render` comes later in the controller action, we need to _early
|
||||
return_ after the redirect to avoid multiple render/redirect warnings.
|
||||
|
||||
It is important to use `and` here instead of `&&` because of logical operator
|
||||
precedence.
|
||||
|
||||
If we used `&&` instead:
|
||||
|
||||
```ruby
|
||||
redirect_to sign_in_path && return if current_user.blank?
|
||||
```
|
||||
|
||||
The `redirect_to` would get `nil` (the result of `sign_in_path && nil`). If we
|
||||
did want to use `&&`, we'd need to be diligent with method call parentheses.
|
||||
|
||||
```ruby
|
||||
redirect_to(sign_in_path) && return if current_user.blank?
|
||||
```
|
||||
|
||||
In this case, `redirect_to` would actually get called with a path string and
|
||||
the `return` would be called after that.
|
||||
|
||||
[source](https://stackoverflow.com/a/37211314/535590)
|
||||
Reference in New Issue
Block a user