All threads / Enable turbo_frame: modal tag conditionally
Ask A Question

Notifications

You’re not receiving notifications from this thread.

Enable turbo_frame: modal tag conditionally

Ahmed Nadar asked in Rails

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?

    Join the discussion

    Want to stay up-to-date with Ruby on Rails?

    Join 68,099+ developers who get early access to new tutorials, screencasts, articles, and more.

      We care about the protection of your data. Read our Privacy Policy.

      logo Created with Sketch.

      Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

      © 2022 GoRails, LLC. All rights reserved.