-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from okuramasafumi/to_json
[Fix] let Rails implicitly call `to_json`
- Loading branch information
Showing
4 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Alba for Rails | ||
author: OKURA Masafumi | ||
--- | ||
|
||
# Alba for Rails | ||
|
||
While Alba is NOT designed for Rails specifically, you can definitely use Alba with Rails. This document describes in detail how to use Alba with Rails to be more productive. | ||
|
||
## Initializer | ||
|
||
You might want to add some configurations to initializer file such as `alba.rb` with something like below: | ||
|
||
```ruby | ||
# alba.rb | ||
Alba.backend = :active_support | ||
Alba.enable_inference!(:active_support) | ||
``` | ||
|
||
You can also use `:oj_rails` for backend if you prefer using Oj. | ||
|
||
## Rendering JSON | ||
|
||
You can render JSON with Rails in two ways. One way is to pass JSON String. | ||
|
||
```ruby | ||
render json: FooResource.new(foo).serialize | ||
``` | ||
|
||
But you can also render JSON passing `Alba::Resource` object. Rails automatically calls `to_json` on a resource. | ||
|
||
```ruby | ||
render json: FooResource.new(foo) | ||
``` | ||
|
||
Note that almost all options given to this `render` are ignored. The only exceptions are `layout`, `prefixes`, `template` and `status`. | ||
|
||
```ruby | ||
# This `only` option is ignored | ||
render json: FooResource.new(foo), only: [:id] | ||
|
||
# This is OK | ||
render json: FooResource.new(foo), status: 200 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters