Skip to content

Commit 8c805dd

Browse files
hegwinjho406
authored andcommitted
docs: add note on layout support for API-only Rails apps
1 parent 2bbf3de commit 8c805dd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,28 @@ json.flash flash.to_h
640640
**NOTE** PropsTemplate inverts the usual Rails rendering flow. PropsTemplate
641641
will render Layout first, then the template when `yield json` is used.
642642

643+
644+
### Layouts in API-only Rails apps
645+
646+
If your controllers inherit from `ActionController::API` (typical in API-only Rails apps),
647+
the layout feature won’t work out of the box, because `ActionController::API`
648+
does not include layout support.
649+
650+
To enable layout rendering, you can include `ActionView::Layouts` manually,
651+
then use `layout "your_own_layout"` as usual:
652+
653+
```ruby
654+
module Api
655+
class BaseController < ActionController::API
656+
include ActionView::Layouts
657+
658+
layout "api"
659+
end
660+
end
661+
```
662+
663+
Without this, Rails will silently skip the layout, which can be tricky to notice.
664+
643665
## Change key format
644666
By default, keys are not formatted. This is intentional. By being explicit with your keys,
645667
it makes your views quicker and more easily diggable when working in JavaScript land.

0 commit comments

Comments
 (0)