Enable turbo_frame: modal tag conditionally

Viewed 34

I have a link for creating a new item

  <li>
    <%= link_to "List an item", new_item_path, data: { turbo_frame: 'modal' } %>
  </li>

This opens a item form in a turbo_frame_tag modal <%= turbo_frame_tag "modal" %>

What I need to do is, allow users to list 3 items only. And if they click on above link it redirect it to root_path with a 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.

0 Answers
Related