From f3e3735c7efe9ba8fcc0e58cda4ee3996e12d38e Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 4 Mar 2025 22:47:31 -0500 Subject: Add quotes search --- app/controllers/quotes_controller.rb | 14 ++++++++++++++ app/models/quote.rb | 9 +++++++++ app/views/quotes/search_form.html.haml | 6 ++++++ 3 files changed, 29 insertions(+) create mode 100644 app/views/quotes/search_form.html.haml (limited to 'app') diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb index d4800cb..468f6ea 100644 --- a/app/controllers/quotes_controller.rb +++ b/app/controllers/quotes_controller.rb @@ -30,6 +30,20 @@ class QuotesController < ApplicationController end end + def search_form + @q = Quote.published.ransack + end + + def search + @quotes = Quote.published.ransack(params[:q]).result(distinct: true).paginate(page: params[:page], per_page: 10) + + respond_to do |format| + format.html { render :list } + format.json { render :json => @quotes } + format.xml { render :xml => @quotes } + end + end + def random picked = Quote.where(state: :published).ids.sample redirect_to quote_url(picked) diff --git a/app/models/quote.rb b/app/models/quote.rb index 3301667..b1d25b4 100644 --- a/app/models/quote.rb +++ b/app/models/quote.rb @@ -36,4 +36,13 @@ class Quote < ApplicationRecord def has_tags? !tags.empty? end + + def self.ransackable_attributes(auth_object = nil) + ["content", "notes"] + end + + def self.ransackable_associations(auth_object = nil) + [] + end + end diff --git a/app/views/quotes/search_form.html.haml b/app/views/quotes/search_form.html.haml new file mode 100644 index 0000000..74cdd20 --- /dev/null +++ b/app/views/quotes/search_form.html.haml @@ -0,0 +1,6 @@ +%h3 Search += search_form_for @q, url: search_quotes_url do |f| + .form-field + = f.label :content_or_notes_cont, "Query:" + = f.search_field :content_or_notes_cont + .form-field= f.submit -- cgit 1.4.1