-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c82dc3
commit 9cfaa2c
Showing
2 changed files
with
62 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,6 @@ Topics | |
turbo_frame.md | ||
turbo_stream.md | ||
real-time-updates.md | ||
multi-format.md | ||
redirect.md | ||
test.md |
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,61 @@ | ||
# Multi-format Response | ||
|
||
In Ruby on Rails, `respond_to` is a method that allows you to define how your controller should respond to different types of requests. | ||
|
||
```ruby | ||
class PostsController < ApplicationController | ||
def create | ||
@post = Post.new(post_params) | ||
|
||
respond_to do |format| | ||
if @post.save | ||
format.turbo_stream { render turbo_stream: turbo_stream.append(@post) } | ||
format.json { render json: @post, status: :created } | ||
format.html { redirect_to @post, notice: 'Post was successfully created.' } | ||
else | ||
format.turbo_stream { render turbo_stream: turbo_stream.replace('new_post', partial: 'posts/form', locals: { post: @post }) } | ||
format.json { render json: @post.errors, status: :unprocessable_entity } | ||
format.html { render :new } | ||
end | ||
end | ||
end | ||
end | ||
``` | ||
|
||
In the above code, developer can return different response format based on request `Accept` header. | ||
|
||
We can do similar approach with `turbo_helper` | ||
|
||
```python | ||
from turbo_helper import ( | ||
ResponseFormat, | ||
response_format, | ||
) | ||
|
||
class TaskCreateView(LoginRequiredMixin, CreateView): | ||
def form_valid(self, form): | ||
response = super().form_valid(form) | ||
request = self.request | ||
messages.success(request, "Created successfully") | ||
|
||
with response_format(request) as resp_format: | ||
if resp_format == ResponseFormat.TurboStream: | ||
return TurboStreamResponse( | ||
render_to_string( | ||
"demo_tasks/partial/task_create_success.turbo_stream.html", | ||
context={ | ||
"form": TaskForm(), | ||
}, | ||
request=self.request, | ||
), | ||
) | ||
else: | ||
return response | ||
``` | ||
|
||
Notes: | ||
|
||
1. If the client `Accept` turbo stream, we return turbo stream response. | ||
2. If not, we return normal HTML response as fallback solution. | ||
3. This is useful when we want to migrate our Django app from normal web page to turbo stream gradually. | ||
4. If you are using Python 3.10+, you can use `match` statement instead of `if` statement. |