I have a Rails project in which we’re replacing a lot of JS with Turbo Drive/Streams and it’s great. However, I’m noticing many controllers beginning to look like so:
class MessageController < AppController
def create
@message = Message.create!(params.require(:message).permit(:content))
respond_to do |format|
format.turbo_stream
format.html { redirect_to messages_url }
else
# other code
end
end
# new show edit update...etc
end
While this works, I feel as though it bloats the method when you’re effectively defining an HTML Ajax response and traditional page reload response in the same controller method - each of which can have an entirely different templated response.
Is there any good convention people are using for separating the types resources out? For example, if I had a JSON API in my rails app, it’s common to have a api
module that namespaces the API controllers, which only deal in JSON.
Are people in the community adding for example a ‘turbo’ module to namespace these endpoints that are only/accepting serving turbo_stream requests? Then in their views, the turbo_stream
requests would be pointed to these endpoints as apposed to the general CRUD controller actions mostly used for routing and redirects?
Of course, I know that this can be done! I’m just curious if there’s any specific reason all the example of implementing turbo_streams online are packed into the traditional controller actions.