From a7791635aa98f012d35f97c3c5288494bbb28e38 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 13 Mar 2024 18:03:43 +0100 Subject: Initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- app/controllers/searches_controller.rb | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 app/controllers/searches_controller.rb (limited to 'app/controllers/searches_controller.rb') diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb new file mode 100644 index 0000000..752ccec --- /dev/null +++ b/app/controllers/searches_controller.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +class SearchesController < ApplicationController + before_action :set_search, only: %i[show edit update destroy] + + def index + current_user.update!(last_search_id: nil) + @search = Search.new + end + + def show + current_user.update!(last_search_id: @search.id) + end + + def new + @search = Search.new + end + + def edit; end + + def create + @search = Search.new(search_params) + @search.user_id = @current_user.id + + if @search.save + redirect_to @search + else + render :new, status: :unprocessable_entity + end + end + + def update + if @search.update(search_params) + redirect_to @search, status: :see_other + else + render :edit, status: :unprocessable_entity + end + end + + def destroy + @search.destroy! + redirect_to searches_url, status: :see_other + end + + def search + @search = Search.new + @search.body = params.permit(:body).fetch(:body, '') + render 'search', partial: true, locals: { items: @search.results } + end + + protected + + def set_search + @search = Search.find(params[:id]) + end + + def search_params + params.require(:search).permit(:name, :body, :shared) + end +end -- cgit v1.2.3