I’m working on a page that shows an item created by a user.
On the page I want to show a small panel that suggests to follow the creator.
So it looks something like this at the moment:
<div>
<h1>Item title</h1>
<%= unless current_user.follows? @item.creator %>
<%= turbo_frame :follow_creator do %>
<%= button_to 'Follow the creator', follow_path(@item.creator) %>
<% end %>
<% end %>
</div>
I only want to show this small panel if the user isn’t already following the creator.
But whenever the user clicks on the follow button, I want to replace the panel with some text that says ‘You are now following the creator’.
I’m confused as to how to structure this in a “sensible way”.
My first angle of attack was to create a separate rails controller for this form, with a view like this:
<%= turbo_frame :follow_creator do %>
<% if current_user.follows? @item.creator %>
You are now following the creator
<% else %>
<%= button_to 'Follow the creator', follow_prompt_follow_path(@item.creator) %>
<% end %>
<% end %>
class FollowPromptController < ApplicationController
def follow
current_user.follow(params[:creator])
redirect_to :index
end
end
Then the item controller can render the follow_prompt
template from within its own.
But then when JavaScript isn’t enabled, submitting the form will just bring the user to a weird page that renders nothing but that dialog. It also feels weird to have to render another controllers template from within another controller.
For progressive enhancement, the form that’s submitted would need to redirect to the same item page, and then show the confirmation message, similar to a flash message. But I can’t really wrap my head around how this would work: How do I pass this faux-flash-state in redirect_to
? Do I need to create a custom follow
action on the item controller?
Any advice would be appreciated!