Prevent users from accessing directly a route that has a turbo frame

Quick question:

I have a route posts/new which is intercepted by a turbo_frame_tag and displays a Modal. All good at this point.

Now if users try to access posts/new directly it will display the modal and of course @posts is nil, which I can just go and define it but that’s not what I want.

What I need is a way to prevent direct access to posts/new — anybody got a way of doing this?

In your specific case of using a modal, I’m assuming that the turbo frame for the modal is also found on the resulting page, triggering the modal.

One benefit of Turbo is that it’s a progressive enhancement, so when in doubt, trust that there’s a way to make that idea work.

Personally, I’d use this: having a param (?modal=true) on the modal turbo_frame’s src, but navigating to posts#new directly doesn’t have the param.

Then, in the presence of that param, serve a variant that wraps the posts#new form in a turbo_frame.

In the absence of that param, serve the default variant without the wrapping turbo_frame.

See this post on reddit with some code examples, where the OP was asking specifically about a problem with TurboModalComponent ViewComponent.

2 Likes

Beautiful, thanks so much. I’ll try that and report back.

1 Like