Coder Social home page Coder Social logo

example_search's Introduction

Step By Step

New App

rails new example_search --database=postgresql

config > database.yml

change database names for development, test, production

Create DB & install Rspec

rails db:create

Add rspec-rails gem into GEMFILE

bundle i

rails g rspec:install

Create Product Scaffold

rails g scaffold Product name price:decimal description:text image_data:text

Create DB seed

products = [
    {
        name: "Bread",
        description: "It's a simple carb.",
        price: 500
    },
    {
        name: "Bananas",
        description: "Lots of magnesium packed into a tube.",
        price: 350
    },
    {
        name: "Canned Tuna",
        description: "Responsibly sourced meat in a can.",
        price: 200
    },
    {
        name: "Chocolate Bar",
        description: "Fatty fat fat fat fatty fat.",
        price: 400
    },
    {
        name: "Gummy Bears",
        description: "Keep chewing away, lose them teeth.",
        price: 500
    },
    {
        name: "Microwaveable Buffalo Wings",
        description: "Great for a night out. At home.",
        price: 1500
    },
    {
        name: "Chicken Stock",
        description: "Whip up a great meal with the power of the cube.",
        price: 650
    },
    {
        name: "Beef Stock",
        description: "Beefy beef beef. Great for sauces.",
        price: 650
    },
    {
        name: "Vegetable Stock",
        description: "Vegetarians have great taste. Bless 'em.'",
        price: 650
    },
    {
        name: "Pork Sausages",
        description: "Chuck 'em on the barbie.",
        price: 1000
    }
]

Product.create!(products){ |product| p product.name }
rails db:migrate

rails db:seed

Create Search Form at Product Index page

<%= form_with scope: :products, method: :get do |f| %>
  <%= f.label :search_product_name %>
  <%= f.text_field :search %>
  <%= f.button :search %>
<% end %>

This is an arbitrary form with a GET method -- will be doing POST soon. If you inspect form element at this stage you'll see:

<form action="/products" accept-charset="UTF-8" data-remote="true" method="get"><input name="utf8" type="hidden" value="โœ“">
  <label for="products_search_product_name">Search product name</label>
  <input type="text" name="products[search]">
  <button name="button" type="submit">search</button>
</form>

The form action gets the route '/products'

Write the Search Logic into the Controller Action 'products#index'

def index
  unless params[:products][:search].present?
    @products = Product.all
  else
    product_name = params[:products][:search]
    # Is essentially querying this:
    # {
    #   products: {
    #     search: 'blablabla'
    #   }
    # }
    @products = Product.where(name: product_name)
  end
end

Write the Search Function into the Product Model

class Product < ApplicationRecord
    def self.search_by_name(search)
        # where(name: product_name) Time to put some SQL to use
        where("LOWER(name) LIKE ?", "%#{search.downcase}%")
        # NOT where("name LIKE #{search}") --> SQL injection - vulnerable
    end
end

example_search's People

Contributors

snikeron avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.