Фильтрация заметок других пользователей в Searchkick rails 5 - PullRequest
0 голосов
/ 13 ноября 2018

Я новичок в Ruby on Rails, работаю над приложением для ноутбука.Я пытаюсь использовать Searchkick, чтобы пользователи могли быстро искать свои заметки.В настоящее время у меня есть 2 пользователя (с помощью устройства devise)

Я только что настроил Searchkick, но когда я ищу слово, которое оба пользователя имеют в своих заметках, в результате отображаются заметки обоих пользователей.Таким образом, пользователь может видеть примечание другого в этом случае, как на рисунке ниже.Notebook

Вот мой notes_controller.rb код:

class NotesController < ApplicationController
  before_action :find_note, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @notes = Note.where(user_id: current_user).order("created_at DESC")
  end

  def search
        if params[:search].present?
            @notes = Note.search(params[:search])
        else
          @notes = Note.all
        end
    end

  def new
    @note = current_user.notes.build
  end

  def create
    @note = current_user.notes.build(note_params)
    if @note.save
      redirect_to root_path, notice: "Note successfully created."
    else
      render 'new'
    end
  end

  def show

  end

  def edit

  end

  def update
    if @note.update(note_params)
      redirect_to note_path(@note), notice: "Note successfully updated."
    else
      render 'edit'
    end
  end

  def destroy
    @note.destroy
    redirect_to root_path, notice: "Note successfully deleted."
  end

  private
  def note_params
    params.require(:note).permit(:title, :body)
  end

  def find_note
    @note = Note.find(params[:id])
  end
end

Мой routes.rb код:

Rails.application.routes.draw do
  devise_for :users
  resources :notes do
    collection do
      get :search
    end
  end

  authenticated :user do
    root "notes#index"
  end

  root "welcome#home"
end

Мой search.html.erb код, который совпадает с index.html.erb код:

<% @notes.each do |note| %>
  <h2><%= link_to note.title, note_path(note) %></h2>
<% end %>

У меня есть ощущение, что мне нужно добавить условное выражение в действие поиска в notes_controller, но это не работает.

Может кто-нибудь помочь, пожалуйста?

Спасибо.

1 Ответ

0 голосов
/ 18 декабря 2018

Это было бы

@notes = Note.search params[:search], where: { user_id: current_user.id }

...