Skip to main content

How to make Search Engine Friendly url in Ruby On Rails?

FreindlyId gem is used to make friendly looking URLs i.e it lets us replace id’s in the URL with the string. It also prevents hacking of the web-page since the id gets hidden by the slug. Before friendly_id:
After friendly_id :

Here are the simple steps to follow:
Step 1: Add gem “friendly_id” to the Gemfile and run “bundle install” on terminal.
A] On an existing application :
Step 2: Run “rails g migration AddSlugToJobs”.
In this case, “slug” is the column name which is adding into existing “jobs” table.
Slug : Slug is the part of the URL. It is the slug which makes the URL user friendly.
Slug rules states that the string in the URL is separated by dash (-). Its rule also states that the letters in the URL are in lowercase.

Step 3: Add the following code to the migration file :

In my application,I have migration file “20180109135747_add_slug_to_jobs.rb” in “db/migrate/20180109135747_add_slug_to_jobs.rb”. In this step I am adding a column name “slug” to the existing table “jobs” and indexing it. In your case, you can add the code to your migration file and replace “:jobs” with your table name.
Step 4: Run “rake db:migrate
Step 5: Add the following code to the model :

In my case, I have added the code in “job.rb” file which is in “app/models/job.rb”. In your case, add the code in your model’s file. In this step, I am extending “FriendlyId” module and using “friendly_id” method with the column name to tell rails which column to be used as a slug.
Step 6: Edit the show action of the controller:

In my application, I have controller “jobs_controller.rb” in app/controllers/jobs_controller.rb. In your case add the following code to the show action of your controller.
Now the friendly_id will be applied to the new records which you create but in order to add friendly_id to the previous records which was created before adding friendly_id gem, follow the next step.
Step 7: run “rails c” enter and run :
Job.find_each(&:save)
Now your application is ready with a functionality of friendly_id. When you will create new record, it’s id in the URL will gets replaced with the slug. In my case , here is my output.

B] On a new project :
Step 2: Run  “rails generate controller Posts index new show destroy ”
Run  “rails generate model Post title:string slug:string body:text ”
Step 3: Add the following line to the migration file :
add_index :posts, :slug, unique: true

In my case, I have added the line in “20180130104525_create_posts.rb” file which is in “db/migrate/20180130104525_create_posts.rb”. Basically, I am adding the index to the “slug” column. In your case add this line in your file in “db/” and replace “:posts” with your table name.
Step 4: Run “rake db:migrate”
Step 5: Add the following code to the model

In my case, I have added the code in “post.rb” file which is in “app/models/post.rb”. In your case add the code in your model’s file. In this step, I am extending “FriendlyId” module and using “friendly_id” method with the column name to tell rails which column to be used as a slug.
Step 6: Edit the show action in the controller.

In my case, I have edited the show action in “post_controller.rb” which is in “app/controllers/post_controller.rb”. In your case edit the show action in your controller.
Now your newly created application is ready with the functionality of friendly_id.

Source: http://www.cryptextechnologies.com/blogs/how-to-make-friendly-url-using-friendly_id-gem

Comments

Popular posts from this blog

GraphQL With Ruby

Now a day’s most of the web or mobile applications fetch data from server which is stored in a database. REST API provides an interface to stored data that require by the applications. GraphQL is a query language for REST API's not for server databases. It is database agnostic and effectively can be used in any context where an API is used. GraphQL provide platform for declarative data fetching where client need to specify what data needs from API in response. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for. GraphQL minimizes the amount of data that needs to be transferred over the network and improves applications operating under these conditions. Introduction to GraphQL API on Ruby on Rails Start with adding gem in Gemfile gem ‘graphql’ Run command bundle install Run command rails generate graphql:install Above command will add gr

Best In Place Gem In Ruby On Rails Tutorial

The best_in_place gem is the easiest solution for in place editing in Ruby on Rails. This gem provides functionality of “in place editing” in ruby on rails without writing any extra ajax code. It supports text inputs, textarea, select dropdown, checkboxes, jQuery UI Datepickers, etc. Also Displays server-side validation Installation Steps of “best_in_place” Gem : Installing best_in_place is very easy and straight-forward. Just begin including the gem in your Gemfile: gem ‘best_in_place’ After that, specify the use of the jquery and best in place javascripts in your application.js, and optionally specify jquery-ui if you want to use jQuery UI datepickers: //= require jquery //= require best_in_place //= require jquery-ui //= require best_in_place.jquery-ui Then, just add a binding to prepare all best in place fields when the document is ready: $(document).ready(function() { /* Activating Best In Place */ jQuery(".best_in_place").best_in_place(); });

Data scraping in Ruby on Rails using Nokogiri and Mechanize Gem

What is Data scraping? Website/Data  Scraping  is a technique to operating large amounts of  data  from websites whereby the  data  is extracted and displayed in own sites or it can be stored to a File/Database. Data scraping is basically used where the websites does not provides API. Some Applications do not provide API to collect records. For the same , Data Scraping technique is used. The data can be scraped using Nokogiri Gem. The steps are required: Add the gem “gem ‘nokogiri’, ‘~> 1.8’, ‘>= 1.8.1'” . Then run the bundle install Add the “require ‘nokogiri'” , “require ‘open-uri'” line where you will write the code for the scraping. The controller of the page will look like below: The view of the code of view page will look like : The result in our application will look like: Mechanize Gem in rails The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follo