ruby-on-railsrubypunditpg-search

adding pg search to index page but having pundit issues


im trying to add pg search to my tweets index page, to search for tweets and users, however im having some pundit issues when i search. I've tried to set my tweet policy to return true on the index page but nothing changes. ive tried to look on pundit documentation, but i cant seem to find anything. thanks. edit: i have added authorize @tweet in the index method but still doesnt workenter image description here

class Tweet < ApplicationRecord
  belongs_to :user
  acts_as_votable
  validates :content,
    presence: true,
    length: {maximum: 50},
    on: :create,
    allow_nil: false

    include PgSearch
  pg_search_scope :global_search,
    against: [ :content ],
    associated_against: {
      user: [ :first_name, :last_name , :username]
    },
    using: {
      tsearch: { prefix: true }
    }
end

<%= form_tag tweets_path, method: :get do %>
  <%= text_field_tag :query,
    params[:query],
    class: "form-control",
    placeholder: "Find a tweet or user"
  %>
<% end %>

   def index
    # @tweets = Tweet.all.order("created_at DESC")
    # @tweets = policy_scope(Tweet).paginate(page: params[:page], per_page: 7).order('created_at DESC')

    if params[:query].present?
      @tweets = Tweet.global_search(params[:query])
    else
    @tweets = policy_scope(Tweet).paginate(page: params[:page], per_page: 7).order('created_at DESC')

    end
    @tweet = Tweet.new
    @user = current_user
  end

class TweetPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      scope.all
    end
  end

  def index?
    return true
  end

  def create?
    return true
  end

  def upvote?
    return true
  end

def downvote?
    return true
  end
  def update?
    record.user == user
    # - record: the restaurant passed to the `authorize` method in controller
    # - user:   the `current_user` signed in with Devise.
  end

  def destroy?
    record.user == user
  end
end


Solution

  • i figured it out. i didnt add policy scope to the other part of the if else statement in the tweets controller index method.