Tuesday, 30 April 2013
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
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
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"
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
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
u.update_attribute(:role, 'admin')
u
u.password
u.role
u.status
etc etc
Friday, 26 April 2013
locales yml changes
In locales .yml file is used for translations
db:migrate up and down
$ rake db:migrate:down VERSION=migration version number e.g. 20130102123
$ rake db:migrate:up VERSION=migration version number e.g. 20130102123
$ rake db:migrate:up VERSION=migration version number e.g. 20130102123
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
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)
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 status
git branch
git pull origin branch_name
rails s
Monday, 22 April 2013
Github Changing Username
- Changing username
to confirm run $ git config --global user.name
Thursday, 4 April 2013
USEFULL STUDY MATERIAL
Usefull study material for ruby and rails
- Programming Ruby 1.9, The pragmatic programmers guide by Dave thomas with chad fowler and Andy hunt.
- 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) ..
- SIMPLY RAILS 2, BY PATRICK LENZ
- Beginning Rails 3, By Cloves Carneiro Jr., and Rida Al Barazi
- The one that I liked the most ...Ruby on Rails 3.0, a free student manual
- Agile web development with Rails, Fourth Edition, for Rails 3.2 , free download link
- 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
- Another RSpec Book. Better Specs
- 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 :
$ 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:
- take the number in second position (3205 in this case)
- 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
$ mysql -u root -p db_name < file_name.sql
like
mysql -u root -p xxx_development < data_migrate_xxx.sql
Subscribe to:
Posts (Atom)