добавление pg search на индексную страницу, но с проблемами в pundit - PullRequest
0 голосов
/ 03 октября 2018

Я пытаюсь добавить pg search к моей странице индекса твитов, чтобы искать твиты и пользователей, однако у меня возникают некоторые проблемы с пандитами при поиске.Я попытался настроить политику твитов так, чтобы она возвращала true на странице индекса, но ничего не изменилось.Я пытался посмотреть на документацию pundit, но я не могу ничего найти.Благодарю.редактировать: я добавил авторизацию @tweet в методе index, но все еще не работает enter 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

1 Ответ

0 голосов
/ 05 октября 2018

Я понял это.я не добавил область действия политики к другой части оператора if else в методе индекса контроллера твитов.

...