Tuesday 30 April 2013

Rake Routes for a specific controller

rake routes | grep controller_name

Customizing JSON API to return few records

just Add the following method in to the Model ...
def as_json(options = {})
    {
      name: self.name,                # Whatever you want to show
      city: self.city,
      country: self.country,
      customer_id: self.customer_id
   
         }
  end
--------------------------OR---------------------------------------
you can call the .select method in the API controller
like
class Api::V1::YoursController < ApplicationController
  def show
  @your = Your.select('one, two').find(params[:id])
  respond_to do |format|
        format.json {render :json => @your}
        format.xml {render :xml => @your}
    end
 end
end

Rails Restfull API/Rails json/xml api

In Controllers => api => V1
create "users_controllers.rb"
the code inside this controller may seems something like following
class Api::V1::HotelsController < ApplicationController
  def show
  @user = user.find( params[:id] )
  respond_to do |format|
        format.json {render :json => @user}
        format.xml {render :xml => @user}
    end
 end
end
_____
Now go to routes and Add the following
namespace :api do
    namespace :v1 do
      resources :users       
    end
   end
____________
now run $ rake routes | grep users
and check the path against the show method. this will seem something like following.
"/api/v1/users/:id"
---
Now hit http://localhost:3000/api/v1/users/(any ID like 1,2 or 3).json

Monday 29 April 2013

Git History => Git log/push or commit history

$ git log
this will show all the commit history.
but now if someone want to search for specific user commit history
$ git log --author="sami"

creatig a sub branch

first remain on the same branch from where you want to create a sub branch
then run the following command:
$ git checkout -b new_branch

Sunday 28 April 2013

working with console => update attribute

u=User.find(id)
u.update_attribute(:role, 'admin')
u
u.password
u.role
u.status
etc etc

Tuesday 23 April 2013

rails generate add and remove migration

rails generate migration add_and_remove_additional_columns_from_user first_name:string last_name:string

rails production environment

rails s -e production

rails assets precompile

rake assets:precompile

Git changing a Branch

git branch
git checkout 'branch name'

making a directory and cloning

cd ..
ls
rm -rf demoapp/  #(this is for removing the current directory)
mkdir demoapp
git clone git@github.com:xyz-abc/lmk-ror.git . #(github ssh goes here)

Git Commands =++ Git Pull

gitg
git status
git branch
git pull origin branch_name
rails s

Git Changing/Renaming Branch Name

git branch -m newname

Monday 22 April 2013

Github Changing Username

  • Changing username
$ git config --global user.name "givename"
to confirm  run $ git config --global user.name

Thursday 4 April 2013

USEFULL STUDY MATERIAL

Usefull study material for ruby and rails
  1.  Programming Ruby 1.9, The pragmatic programmers guide by Dave thomas with chad fowler and Andy hunt.
  2. Roy Thomas Fielding PhD thesis on "Architectural Styles and the Design of Network-based Software Architectures" (the basic concept of RESTful architecture was presented by Thomas for the first time) ..
  3. SIMPLY RAILS 2, BY PATRICK LENZ
  4. Beginning Rails 3, By Cloves Carneiro Jr., and Rida Al Barazi
  5. The one that I liked the most ...Ruby on Rails 3.0, a free student manual
  6. Agile web development with Rails, Fourth Edition, for Rails 3.2 , free download link
  7. The RSpec Book, behavior driven development with RSpec, Cucumber and Friends By David Chelimsky with Dave Astels, Zach Dennis, Aslak Hellesøy, Bryan Helmkamp and Dan North, free download Link
  8. Another RSpec Book. Better Specs
  9. Learning JavaScript Design Patterns by Addy Osmani. Link 

Rails 3.2 MULTIPLE SERVER ISSUE

rails 3.2 do not allow to run multiple servers to resolve this issue the following command can be used ..

$ bundle exec rails s -p 3001 -P tmp/pids/server2.pid
___________________
Another Solution is to follow the steps below :
  • $ lsof|grep 3000

  • this will give you a line starting with:
                    ruby   3205   sami   8u  IPv4
  • take the number in second position (3205 in this case) 
and in console do:
  • kill -9 6205

RAILS DATABASE IMPORT THROUGH CONSOLE

RAILS DATABASE IMPORT
$ mysql -u root -p db_name < file_name.sql
like
mysql -u root -p xxx_development < data_migrate_xxx.sql