Enable turbo_frame: modal tag conditionally
I have a link for creating a new item
<%= link_to "List an item", new_item_path, data: { turbo_frame: 'modal' } %>
This opens a item form in a turbo_frame_tag modal <%= turbo_frame_tag "modal" %>
What I need to do is, allow regular users to list 3 items only while Admin can list more items. And if they click on above link it redirect it to root_path with a flash notice.
I have such validation in item controller:
before_action :check_limited_items, if: :signed_in?, only: :new
def check_limited_items
limited_items = 3
return unless current_user.items_count == limited_items
redirect_to root_path
flash[:notice] = "Allow 3 items only."
end
When I click on above link for a new item, it does nothing. When i refresh, flash notice shows up.
How could I fix that? And is a controller a good place for validation or model? I tried both and same result.
Also, I use Pundit for authorizations, can it help using scopes?