Friday 3 July 2015

How to Use Beautified_url Gem in Ruby on Rails Application

Beautified Url Basic tool which provides feature of generating tokens which can be used in url for identifying resource(uniquely of uniquely within a scope) instead of :id. Just add another field which starts with _bu_ of one or many existing fields to make it all happen. Example: For a blog post, if you have a 'title' field and want to beautify and use it .

Simple way to implement ..
1. Adding gem  in your Gem-file.

 gem 'beautified_url', '~> 0.0.2'
 

2. Now you have to create a migration(adding a column  for _bu_ url ) on model for beautified Url.
the name of column is start with _bu_ .


class AddingBfColumnInUsers < ActiveRecord::Migration

 def change 
   add_column :users, :_bu_name, :string, after: 'full_name' 
 end 
end
---------------------------------------------------------------
class AddingBfColumnInProjects < ActiveRecord::Migration

 def change
  add_column :projects, :_bu_name, :string, after: 'name' 
 end 
end

3. Now last step is create routes for the resources

get ':_bu_full_name/:_bu_name', to: 'projects#show', as: :show_project
get ':_bu_full_name/:_bu_name/edit', to: 'projects#edit' , as: :edit_project


NOTE : If prams not found then You have also pass _bu_name and _full_name (user full name ) as prams through model by overriding to_prams default methods.

class Project < ActiveRecord::Base 

  def to_param 
    _bu_name.to_s 
  end 
end 




No comments:

Salesforce CRM vs. Zoho: A Comparative Analysis

Introduction: Selecting the right customer relationship management (CRM) software is crucial for businesses seeking to streamline their sal...