Filtering out other users's notes in Searchkick rails 5











up vote
0
down vote

favorite












I'm a beginner to Ruby on Rails working on a Notebook app. I'm trying using Searchkick to enable users to quickly search their notes. I currently have 2 users (via devise gem).



I have just set up Searchkick, but when I search for a word that both the users have in their notes, the result shows notes by both users. So, a user can see the other's note in this case as in the image below.
Notebook



Here is my notes_controller.rb code:



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


My routes.rb code:



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


My search.html.erb code, which is the same as index.html.erb code:



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


I have a feeling I need to add a conditional statement in the search action in the notes_controller but that is not working.



Can anyone help please?



Thank you.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm a beginner to Ruby on Rails working on a Notebook app. I'm trying using Searchkick to enable users to quickly search their notes. I currently have 2 users (via devise gem).



    I have just set up Searchkick, but when I search for a word that both the users have in their notes, the result shows notes by both users. So, a user can see the other's note in this case as in the image below.
    Notebook



    Here is my notes_controller.rb code:



    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


    My routes.rb code:



    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


    My search.html.erb code, which is the same as index.html.erb code:



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


    I have a feeling I need to add a conditional statement in the search action in the notes_controller but that is not working.



    Can anyone help please?



    Thank you.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm a beginner to Ruby on Rails working on a Notebook app. I'm trying using Searchkick to enable users to quickly search their notes. I currently have 2 users (via devise gem).



      I have just set up Searchkick, but when I search for a word that both the users have in their notes, the result shows notes by both users. So, a user can see the other's note in this case as in the image below.
      Notebook



      Here is my notes_controller.rb code:



      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


      My routes.rb code:



      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


      My search.html.erb code, which is the same as index.html.erb code:



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


      I have a feeling I need to add a conditional statement in the search action in the notes_controller but that is not working.



      Can anyone help please?



      Thank you.










      share|improve this question













      I'm a beginner to Ruby on Rails working on a Notebook app. I'm trying using Searchkick to enable users to quickly search their notes. I currently have 2 users (via devise gem).



      I have just set up Searchkick, but when I search for a word that both the users have in their notes, the result shows notes by both users. So, a user can see the other's note in this case as in the image below.
      Notebook



      Here is my notes_controller.rb code:



      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


      My routes.rb code:



      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


      My search.html.erb code, which is the same as index.html.erb code:



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


      I have a feeling I need to add a conditional statement in the search action in the notes_controller but that is not working.



      Can anyone help please?



      Thank you.







      ruby-on-rails elasticsearch searchkick






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 at 0:18









      jerof

      217




      217





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271997%2ffiltering-out-other-userss-notes-in-searchkick-rails-5%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271997%2ffiltering-out-other-userss-notes-in-searchkick-rails-5%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?