Filtering out other users's notes in Searchkick rails 5












0














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



























    0














    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

























      0












      0








      0







      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 '18 at 0:18









      jerofjerof

      237




      237
























          1 Answer
          1






          active

          oldest

          votes


















          0














          That would be



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






          share|improve this answer





















            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',
            autoActivateHeartbeat: false,
            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

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            That would be



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






            share|improve this answer


























              0














              That would be



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






              share|improve this answer
























                0












                0








                0






                That would be



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






                share|improve this answer












                That would be



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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 18 '18 at 15:46









                Andrew RozhenkoAndrew Rozhenko

                365




                365






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini