Wednesday 3 October 2012

Rails Console Tips



1.To call Applicationcontroller methods from the Console



    ApplicationController.new.methods
    => :hello, :current_user, :create_support_user

          It will display all the methods of application controller


    ApplicationController.new.hello
    => "Hello World"

          Here :hello is the method defined inside application_controller.

     Note: Instead of using ApplicationController.new you can also use controller.methods 
     Check this link for more details related to controller 

2. To use Helper methods from the Console


 
    ApplicationController.helpers.methods
    => :admin?, :client?   
     OR
    helper.methods
    => :admin?, :client?    

         It will display all the helper methods
   
    helper.number_to_currency(100)
    => "$100.00"
    helper.truncate("Testing User", length: 2)
    => "Testing Use..."
    helper.pluralize(2, "Person")
    => "2 People" 


3. Who defined helper?

    
    helper.method(:truncate)
    => #<Method: ActionView::Base(ActionView::Helpers::TextHelper)#truncate> 
    helper.method(:link_to)
    => #<Method: ActionView::Base(ActionView::Helpers::UrlHelper)#link_to> 
    helper.method(:number_to_currency)
    => #<Method: ActionView::Base(ActionView::Helpers::NumberHelper)#number_to_currency 


4. app

    
    app.class
    => ActionDispatch::Integration::Session

  i. Routes
  
    app.overview_admin_index_path()
    => "/admin/overview"   
    app.edit_user_registration_path(User.first)
    =>/users/edit.12" 

  ii. Url

    app.url_for(:controller => :admin)
    => "http://www.example.com/admin" 
    app.url_for(:controller => :admin, :action => :overview)
    => "http://www.example.com/admin/overview"

  iii. Submit request 
    To make request from console

   app.get "/users/sign_in"
   => 200

   app.session
   => {"session_id"=>"786731e9880c620cf638b57a4db88a1e", "user_return_to"=>"/admin/overview", "flash"=>#, @closed=false, @flashes={:alert=>"You need to sign in or sign up before continuing."}, @now=nil>, "_csrf_token"=>"kvMbabanAsi07gEfsuzHdR7xnCEz0hMIvRBU2LiaqFs="} 
 
   app.get "/users/sign_in",{:username => 'new_user', :password => '123456789'}
   => 200

   app.cookies
   => #<Rack::Test::CookieJar:0xbf005a4 @default_host="www.example.com", @cookies=[#<Rack::Test::Cookie:0xba68e24 @default_host="www.example.com", @name_value_raw="_snipper_host_india_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTc4NjczMWU5ODgwYzYyMGNmNjM4YjU3YTRkYjg4YTFlBjsAVEkiE3VzZXJfcmV0dXJuX3RvBjsARkkiFC9hZG1pbi9vdmVydmlldwY7AEZJIhBfY3NyZl90b2tlbgY7AEZJIjFrdk1iYWJhbkFzaTA3Z0Vmc3V6SGRSN3huQ0V6MGhNSXZSQlUyTGlhcUZzPQY7AEY%3D--152f06b94ca610d662cfd7148b7a4a49ceac23eb", @name="_snipper_host_india_session", @value="BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTc4NjczMWU5ODgwYzYyMGNmNjM4YjU3YTRkYjg4YTFlBjsAVEkiE3VzZXJfcmV0dXJuX3RvBjsARkkiFC9hZG1pbi9vdmVydmlldwY7AEZJIhBfY3NyZl90b2tlbgY7AEZJIjFrdk1iYWJhbkFzaTA3Z0Vmc3V6SGRSN3huQ0V6MGhNSXZSQlUyTGlhcUZzPQY7AEY=--152f06b94ca610d662cfd7148b7a4a49ceac23eb", @options={"path"=>"/", "HttpOnly"=>nil, "domain"=>"www.example.com"}>]>


  iv. Response

   app.response
     It will display full html code

   app.response.response_code
   => 200

   app.response.body[0..100]
   => "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml"

   app.path
   => "/users/sign_in"


4. Method location

      To get exact source loaction of particular method using Method#source_location

    User.instance_method(:role?).source_location
    =>["/home/Desktop/Workspace/host_india/app/models/user.rb", 32] 

     Let's try for app

    app.method(:post).source_location
    => ["/home/gvo/.rvm/gems/ruby-1.9.2-p318/gems/actionpack-3.2.6/lib/action_dispatch/testing/integration.rb", 38] 


5. Accessing the last return value


   "Hello"
   => "Hello" 
   str = _
   => "Hello" 
   str
   => "Hello" 

   Commission.first
   => #
   _.amount
   => 4947.3

1 comment:

  1. This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.
    Purchase Console in Nepal

    ReplyDelete