James Ellis

James Ellis

1565142371

Build a Simple Search with the simple_form Gem in Rails 5

A search can be a basic filter of your model, but creating a search that is efficient, fast, and actually gets the results you want to show is much more complex. Think of how you felt last time you searched for something on Bing instead of Google. A good algorithm can make all the difference.

Follow

I’ve seen many search tutorials for Rails 5 that use form_tag , HTML forms, or form_for, but there are not as many resources for simple_form, even though simple_form has become the gold standard of Rails forms. Besides, both form_for and form_tag are on their way to be deprecated after Rails 5.2 and replaced by form_with.

Here’s a quick overview of how you can add basic search functionality to your app using the simple_form gem!

Let’s get started!

1. Setup your Rails App

I’m creating a basic rails application, without Webpacker, but using PostgreSQL as my database, because it will allow me to do case-insensitive search later on.

rails new simple_search --database=postgresql 
rails db:create 

p.s. Remember to create the database after you make your rails app because postgres doesn’t create a db by default

#Gemfile
gem 'simple_form'
gem 'bootstrap', '~> 4.2.1' 
bundle install

p.s. Don’t forget the extra steps to setup Bootstrap & configure SimpleForm!

2. Create the model

For this case, I’m going to create a simple one-model app to search through a list of cocktails that I added to my database via a JSON endpoint (thanks to github.com/teijo for the db!). I’ll go through the data import quickly. If you want a full article on seeding from a third party database, leave a message below!

First create the model with three attributes:

rails g model cocktail name glass preparation:text
rails db:migrate

Then add the seeds:

#db/seeds.rb
require 'json'
require 'open-uri'
url = "https://raw.githubusercontent.com/maltyeva/iba-cocktails/master/recipes.json"
Cocktail.delete_all if Rails.env.development?
cocktails = JSON.parse(open(url).read)
cocktails.each do |cocktail|
  Cocktail.create!(name: cocktail["name"], glass: cocktail["glass"], preparation: cocktail["preparation"])
end

3. Create the controller & views

Next we’re adding a controller with a single action, to show a list of all the cocktails. Since our app has one page, I’m going to root the application to the cocktails index. There is no /cocktails path in the application.

First, create the controller:

#terminal 
rails g controller cocktails index
#app/controllers/cocktails_controller.rb
class CocktailsController < ApplicationController
  def index
   @cocktails = Cocktail.all
  end
end

Then, add a home route for the index:

#config/routes.rb 
root to: "cocktails#index"

Lastly, let’s add a simple front-end for the cocktail:

#app/views/cocktails/index.html.erb
<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-8 offset-sm-2">
      <h1>Cocktails</h1>
      <% @cocktails.each do |cocktail| %>
        <h3><%= cocktail.name %></h3>
        <p><%= cocktail.preparation %></p>
      <% end %>
    </div>
  </div>
</div>

4. Add the search form

Now it’s time to add the search functionality. I love simple_form because of… well… its simplicity, because you can make a form in just four lines. Check out the full documentation here.

#app/views/cocktails/index.html.erb
<%= simple_form_for :search, url: root_path, method: "GET", html: { class: 'form-inline' } do |f| %>
  <%= f.input :name, input_html: {value: ""} %>   
  <%= f.submit "Search", class: "btn btn-primary" %>
<% end %>

There’s a few things to watch out for here.

  1. The first thing is the :search . Typically, we create the form for an instance variable when manipulating a model (i.e. @cocktail) ). If I try to call the parameter @search, the application will tell me that the variable is undefined. By setting it as a symbol with the colon before :search, I can use it as a key in my params hash.
  2. The url of the search form tells us on which page the results will be displayed. Here I specified it as the root_path. The application only has one page (the root page of the cocktails index), so I need to tell my form to show the results on that page.
  3. By default, a simple_form has the method POST. Since a search is a GET request, we need to explicitly specify that in the first line.
  4. The input :name is going to be a key inside of my params hash. The value is the keyword the user searches for. In order to reset the form on page refresh, I am giving it a default value of blank.

5. Query your search in the controller

We’ll start with a super simple search — an exact keyword. This is a basic query, but not really useful, because it won’t find Margarita if my search phrase is margarita, or any part of the word.

But for now, let’s start with the basics:

class CocktailsController < ApplicationController
  def index
    @cocktails = Cocktail.all
    @search = params["search"]
    if @search.present?
      @name = @search["name"]
      @cocktails = Cocktail.where(name: @name)
    end
  end
end

6. Let’s make this search better!

The first thing I want to do is make my search case-insensitive, so Margarita and margarita return the same result.

Here, my simple active record query won’t do and I’ll need to add SQL. One of the reasons I started the app with PostgreSQL as my db adapter is to use ILIKE, which will give me the case-insensitive match:

@cocktails = Cocktail.where("name ILIKE ?", @name)

What about a partial search? What if we want Margarita to return all margarita recipes like Passionfruit Margarita or Strawberry Margarita? We’ll need to add wildcard rules to make that work. Note that to add the % wildcard symbol, I need to interpolate my variable as a string.

@cocktails = Cocktail.where("name ILIKE ?", "%#{@name}%")

8. That’s it!

We’re done with our basic search. There’s lots of things I can add to this app, like searching through multiple columns, multiple tables, saving my search in the session, or using scopes to make the search more efficient. Let me know below if you’d like an article on advanced search!

GitHub Repository here.

Heroku Demo here.

Further reading:

How To Use PostgreSQL with Your Ruby on Rails Application on macOS
https://morioh.com/p/09f1e543c728

Ruby For Beginners: Learn to Code with Ruby from Scratch
http://class.learnstartup.net/p/GgFNUm4tP

Ruby Metaprogramming Is Even Cooler Than It Sounds
https://morioh.com/p/0116c0dcd0fe/

Ruby on Rails Tutorial: Learn 6 Ruby on Rails SEO Techniques
http://class.learnstartup.net/p/CZBBMrl-_

Ruby Algorithm Documentation with AsciiDoc and Knitr
https://morioh.com/p/b104565ecc94/

Ruby Programming for Beginners
http://class.learnstartup.net/p/vjkBeGb2M

#ruby-on-rails #ruby

What is GEEK

Buddha Community

Build a Simple Search with the simple_form Gem in Rails 5
Marcelle  Smith

Marcelle Smith

1598839653

Authenticating Your API Using "Knock Gem" in Rails

Due to the inability to generate a well-defined way to authenticate rails API, I have sourced out this information in order to help me and you have a way of authenticating our rails API token. This tutorial would be based on the latest version (6.0) of Ruby on Rails.

In getting this set up as well as up and doing, the few steps through which the processes would be implemented are listed below:

Step 1:

We would need to, first of all, generate a new rails app as we embark on the journey. Follow the example given below:

$ rails new sample-app --api -d=postgresql -T
$ cd sample-app
$ rails db:create
$ rails db:migrate

Step 2:

In order to gain access to the API, we would need to uncomment or remove Cors gems as that serves as permission into gaining access to the API

gem “rack-cors”

After uncommented the gem ‘rack-cors’ then we run this command below

$ bundle install

#ruby-on-rails #rails #rails-api #knock-gem #ruby #software-development #rails-only-api

James Ellis

James Ellis

1565142371

Build a Simple Search with the simple_form Gem in Rails 5

A search can be a basic filter of your model, but creating a search that is efficient, fast, and actually gets the results you want to show is much more complex. Think of how you felt last time you searched for something on Bing instead of Google. A good algorithm can make all the difference.

Follow

I’ve seen many search tutorials for Rails 5 that use form_tag , HTML forms, or form_for, but there are not as many resources for simple_form, even though simple_form has become the gold standard of Rails forms. Besides, both form_for and form_tag are on their way to be deprecated after Rails 5.2 and replaced by form_with.

Here’s a quick overview of how you can add basic search functionality to your app using the simple_form gem!

Let’s get started!

1. Setup your Rails App

I’m creating a basic rails application, without Webpacker, but using PostgreSQL as my database, because it will allow me to do case-insensitive search later on.

rails new simple_search --database=postgresql 
rails db:create 

p.s. Remember to create the database after you make your rails app because postgres doesn’t create a db by default

#Gemfile
gem 'simple_form'
gem 'bootstrap', '~> 4.2.1' 
bundle install

p.s. Don’t forget the extra steps to setup Bootstrap & configure SimpleForm!

2. Create the model

For this case, I’m going to create a simple one-model app to search through a list of cocktails that I added to my database via a JSON endpoint (thanks to github.com/teijo for the db!). I’ll go through the data import quickly. If you want a full article on seeding from a third party database, leave a message below!

First create the model with three attributes:

rails g model cocktail name glass preparation:text
rails db:migrate

Then add the seeds:

#db/seeds.rb
require 'json'
require 'open-uri'
url = "https://raw.githubusercontent.com/maltyeva/iba-cocktails/master/recipes.json"
Cocktail.delete_all if Rails.env.development?
cocktails = JSON.parse(open(url).read)
cocktails.each do |cocktail|
  Cocktail.create!(name: cocktail["name"], glass: cocktail["glass"], preparation: cocktail["preparation"])
end

3. Create the controller & views

Next we’re adding a controller with a single action, to show a list of all the cocktails. Since our app has one page, I’m going to root the application to the cocktails index. There is no /cocktails path in the application.

First, create the controller:

#terminal 
rails g controller cocktails index
#app/controllers/cocktails_controller.rb
class CocktailsController < ApplicationController
  def index
   @cocktails = Cocktail.all
  end
end

Then, add a home route for the index:

#config/routes.rb 
root to: "cocktails#index"

Lastly, let’s add a simple front-end for the cocktail:

#app/views/cocktails/index.html.erb
<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-8 offset-sm-2">
      <h1>Cocktails</h1>
      <% @cocktails.each do |cocktail| %>
        <h3><%= cocktail.name %></h3>
        <p><%= cocktail.preparation %></p>
      <% end %>
    </div>
  </div>
</div>

4. Add the search form

Now it’s time to add the search functionality. I love simple_form because of… well… its simplicity, because you can make a form in just four lines. Check out the full documentation here.

#app/views/cocktails/index.html.erb
<%= simple_form_for :search, url: root_path, method: "GET", html: { class: 'form-inline' } do |f| %>
  <%= f.input :name, input_html: {value: ""} %>   
  <%= f.submit "Search", class: "btn btn-primary" %>
<% end %>

There’s a few things to watch out for here.

  1. The first thing is the :search . Typically, we create the form for an instance variable when manipulating a model (i.e. @cocktail) ). If I try to call the parameter @search, the application will tell me that the variable is undefined. By setting it as a symbol with the colon before :search, I can use it as a key in my params hash.
  2. The url of the search form tells us on which page the results will be displayed. Here I specified it as the root_path. The application only has one page (the root page of the cocktails index), so I need to tell my form to show the results on that page.
  3. By default, a simple_form has the method POST. Since a search is a GET request, we need to explicitly specify that in the first line.
  4. The input :name is going to be a key inside of my params hash. The value is the keyword the user searches for. In order to reset the form on page refresh, I am giving it a default value of blank.

5. Query your search in the controller

We’ll start with a super simple search — an exact keyword. This is a basic query, but not really useful, because it won’t find Margarita if my search phrase is margarita, or any part of the word.

But for now, let’s start with the basics:

class CocktailsController < ApplicationController
  def index
    @cocktails = Cocktail.all
    @search = params["search"]
    if @search.present?
      @name = @search["name"]
      @cocktails = Cocktail.where(name: @name)
    end
  end
end

6. Let’s make this search better!

The first thing I want to do is make my search case-insensitive, so Margarita and margarita return the same result.

Here, my simple active record query won’t do and I’ll need to add SQL. One of the reasons I started the app with PostgreSQL as my db adapter is to use ILIKE, which will give me the case-insensitive match:

@cocktails = Cocktail.where("name ILIKE ?", @name)

What about a partial search? What if we want Margarita to return all margarita recipes like Passionfruit Margarita or Strawberry Margarita? We’ll need to add wildcard rules to make that work. Note that to add the % wildcard symbol, I need to interpolate my variable as a string.

@cocktails = Cocktail.where("name ILIKE ?", "%#{@name}%")

8. That’s it!

We’re done with our basic search. There’s lots of things I can add to this app, like searching through multiple columns, multiple tables, saving my search in the session, or using scopes to make the search more efficient. Let me know below if you’d like an article on advanced search!

GitHub Repository here.

Heroku Demo here.

Further reading:

How To Use PostgreSQL with Your Ruby on Rails Application on macOS
https://morioh.com/p/09f1e543c728

Ruby For Beginners: Learn to Code with Ruby from Scratch
http://class.learnstartup.net/p/GgFNUm4tP

Ruby Metaprogramming Is Even Cooler Than It Sounds
https://morioh.com/p/0116c0dcd0fe/

Ruby on Rails Tutorial: Learn 6 Ruby on Rails SEO Techniques
http://class.learnstartup.net/p/CZBBMrl-_

Ruby Algorithm Documentation with AsciiDoc and Knitr
https://morioh.com/p/b104565ecc94/

Ruby Programming for Beginners
http://class.learnstartup.net/p/vjkBeGb2M

#ruby-on-rails #ruby

Ruby on Rails Development Services | Ruby on Rails Development

Ruby on Rails is a development tool that offers Web & Mobile App Developers a structure for all the codes they write resulting in time-saving with all the common repetitive tasks during the development stage.

Want to build a Website or Mobile App with Ruby on Rails Framework

Connect with WebClues Infotech, the top Web & Mobile App development company that has served more than 600 clients worldwide. After serving them with our services WebClues Infotech is ready to serve you in fulfilling your Web & Mobile App Development Requirements.

Want to know more about development on the Ruby on Rails framework?

Visit: https://www.webcluesinfotech.com/ruby-on-rails-development/

Share your requirements https://www.webcluesinfotech.com/contact-us/

View Portfolio https://www.webcluesinfotech.com/portfolio/

#ruby on rails development services #ruby on rails development #ruby on rails web development company #ruby on rails development company #hire ruby on rails developer #hire ruby on rails developers

Ethen Ellen

1619519725

Immediate $olution to Fix AOL Blerk Error Code 5 with easy instructions

This is image title

AOL Email is one of the leading web email services. It has a number of features who access easily at any place. Through this, you can easily share messages, documents or files, etc.AOL Blerk Error is not a big issue. It is a temporary error and it occurs when there is an issue in loading messages from the AOL server. If your mind is stuck, How to Resolve or Fix AOL Blerk Error Code 5? Here, In this article, we mentioned troubleshooting steps to fix AOL Blerk Error Code 5.

What are the causes of AOL Blerk Error Code 5?

AOL mail usually presents an AOL Blerk Error 5 after the AOL connection details have been entered. meaning. Your password and your username. This error is usually found in words! Or 'BLERK! Error 5 Authentication problem, 'Your sign-in has been received.

Some of the reasons for the error are as follows:
• Internet browser configuration problem

• Saved erroneous bookmark addresses

• browser cache or cookie

• An AOL Desktop Gold technical error.
How to Fix AOL Mail Blerk Error 5 in a Simple Way

This type of error is mostly due to your browser settings or the use of outdated, obsolete software. Users should remember that the steps to solve problems vary, depending on the browser you are using. Here are the steps to fix the mistake, check your browser and follow the steps.

Internet Explorer: Make sure you use the most recent web browser version. Open a new window and follow the “Tools> Web Options> Security> Internet Zone” thread. Activate ‘Safeguard Mode’ and follow the steps to include AOL Mail in the list of assured websites. Start the browser again to save changes and run Internet Explorer without additional information.
Firefox Mozilla: Open a new Firefox window and press Menu. To start the browser in safe mode, disable the add-on and choose the option to restart Firefox. You can see two options in the dialog box. Use the “Start in Safe Mode” option to disable all themes and extensions. The browser also turns off the hardware speed and resets the toolbar. You should be able to execute AOL mail when this happens.

Google Chrome: Update to the latest version of Chrome. Open the browser and go to the Advanced Options section. Go to ‘Security and Privacy’ and close the appropriate add-ons. Once the browsing history is deleted, the password, cookies saved and the cache will be cleared. Restart your system and try to log in to your AOL account with a new window.

Safari: Some pop-up windows block AOL mail when it comes to Safari and causes authentication issues. To fix the error, use Safari Security Preferences to enable the pop-up window and disable the security warning.

If you see, even when you change the required browser settings, the black error will not disappear, you can consult a skilled professional and see all the AOL email customer support numbers.

Get Connect to Fix Blerk Error Even After Clearing Cache & Cookies?
Somehow you can contact AOL technical support directly and get immediate help if you still get the error. Call +1(888)857-5157 to receive assistance from the AOL technical support team.

Source: https://email-expert247.blogspot.com/2021/04/immediate-olution-to-fix-aol-blerk.html “How to Resolve or Fix AOL Blerk Error Code 5”)**? Here, In this article, we mentioned troubleshooting steps to fix AOL Blerk Error Code 5.

What are the causes of AOL Blerk Error Code 5?

AOL mail usually presents an AOL Blerk Error 5 after the AOL connection details have been entered. meaning. Your password and your username. This error is usually found in words! Or 'BLERK! Error 5 Authentication problem, 'Your sign-in has been received.

Some of the reasons for the error are as follows:
• Internet browser configuration problem

• Saved erroneous bookmark addresses

• browser cache or cookie

• An AOL Desktop Gold technical error.
How to Fix AOL Mail Blerk Error 5 in a Simple Way

This type of error is mostly due to your browser settings or the use of outdated, obsolete software. Users should remember that the steps to solve problems vary, depending on the browser you are using. Here are the steps to fix the mistake, check your browser and follow the steps.

  1. Internet Explorer: Make sure you use the most recent web browser version. Open a new window and follow the “Tools> Web Options> Security> Internet Zone” thread. Activate ‘Safeguard Mode’ and follow the steps to include AOL Mail in the list of assured websites. Start the browser again to save changes and run Internet Explorer without additional information.

  2. Firefox Mozilla: Open a new Firefox window and press Menu. To start the browser in safe mode, disable the add-on and choose the option to restart Firefox. You can see two options in the dialog box. Use the “Start in Safe Mode” option to disable all themes and extensions. The browser also turns off the hardware speed and resets the toolbar. You should be able to execute AOL mail when this happens.

  3. Google Chrome: Update to the latest version of Chrome. Open the browser and go to the Advanced Options section. Go to ‘Security and Privacy’ and close the appropriate add-ons. Once the browsing history is deleted, the password, cookies saved and the cache will be cleared. Restart your system and try to log in to your AOL account with a new window.

  4. Safari: Some pop-up windows block AOL mail when it comes to Safari and causes authentication issues. To fix the error, use Safari Security Preferences to enable the pop-up window and disable the security warning.

If you see, even when you change the required browser settings, the black error will not disappear, you can consult a skilled professional and see all the AOL email customer support numbers.

Get Connect to Fix Blerk Error Even After Clearing Cache & Cookies?

Somehow you can contact AOL technical support directly and get immediate help if you still get the error. Call +1(888)857-5157 to receive assistance from the AOL technical support team.

Source: https://email-expert247.blogspot.com/2021/04/immediate-olution-to-fix-aol-blerk.html

#aol blerk error code 5 #aol blerk error 5 #aol mail blerk error code 5 #aol mail blerk error 5 #aol error code 5 #aol error 5

I am Developer

1597475640

Laravel 7 Full Text Search MySQL

Here, I will show you how to create full text search in laravel app. You just follow the below easy steps and create full text search with mysql db in laravel.

Laravel 7 Full Text Search Mysql

Let’s start laravel full-text search implementation in laravel 7, 6 versions:

  1. Step 1: Install Laravel New App
  2. Step 2: Configuration DB .evn file
  3. Step 3: Run Migration
  4. Step 4: Install Full Text Search Package
  5. Step 5: Add Fake Records in DB
  6. Step 6: Add Routes,
  7. Step 7: Create Controller
  8. Step 8: Create Blade View
  9. Step 9: Start Development Server

https://www.tutsmake.com/laravel-full-text-search-tutorial/

#laravel full text search mysql #laravel full text search query #mysql full text search in laravel #full text search in laravel 6 #full text search in laravel 7 #using full text search in laravel