I have a Turbo Frame on my page which uses the src attribute to load in /chats/. Within this frame I want to be able to know whether the main page is using the show action of the groups controller, i.e. the URL of the page is at /groups/group_name.
Using current_page?(controller: 'groups', action: 'show') returns false as it sees itself as in the chats controller. How do I work around this?
Here are the options I've found:
request.referrerThere doesn't appear to be a built-in way of getting to the controller class / action in the way that you're describing, but you can access the URL of the page which initiated the Turbo request (groups_controller#show) through
request.referrer. This will be the fully-qualified URL for the page, such ashttp://localhost:3000/groups/1/show.This requires changes to view code (you must add query params to all links where you need this functionality), but it allows you to pass controller/action names and any other arbitrary data you'd like.
Example:
in application_controller.rb:
No need to touch the groups controller in this example.
in show.html.erb (the page which submits the Turbo request)
chats partial controller (which handles the Turbo request)
flashI've just learned the many ways you can use
flashfor this type of functionality that extends across requests. This is less work than using query parameters, mainly because you don't need to adjust your view code.Example:
groups controller (which renders the show view, which submits the Turbo requests)
chats partial controller (which handles the Turbo requests)