Saturday, 14 February 2015

Use of respond to in rails 4




Its a way of responding to the client based on what they are asking for, if the client asks for HTML, Rails will send back HTML to the client, if they ask for XML then XML and also Json. 

By using respond to in rails we present the data or we sending the data according to client requirement by this method.

Example : 

 respond_to do |format|

     format.html { redirect_to(person_list_url}     
     format.xml  {render :xml => @modelname }
     format.json {render :json => @modelname.as_json}
     format.js {}

end


Here we send data in multiple format according to client requirement.  but in html format we redirect the url for html request for a particular page..in html format.

For more information go on =>  respond_to in rails 4 .








Saturday, 7 February 2015

what is mime type and respond_to in rails

respond_to:    and  MIME Type.
--------------------------------------------------------

respond_to is a method of rails that defines different formats that your actions, well , respond to.
And the formate of file is maintained in rails mime_type.

#require 'action_controller/mine_types'

#actionpack/lib/actioncontroller/mimetypes.rb 

 and if you need to use a MIME type which isn't supported by default, you can register your own handler in config/initializers/mime_types.rb

MIME::Type.register "image/jpg", :jpg

And Respond to also allows you to specify a common block for different formats by using any:

def index
  @people = Person.all

  respond_to do |format|
    format.html
    format.any(:xml, :json) { render request.format.to_sym => @people }
  end
end
here we see the block of respond_to is use for render different formats by using do block.

Now! one thing is important to notice the render file extension should be like this..
according to this block .. on index action it will be render two file ,
1st -- index.xml.erb, and 2nd is index.json.erb .






 

Friday, 6 February 2015

Basic Git Command

Git is use for VCS ( Version Control System). To use for records changes to a file or set of files over time so that you can recall specific versions according to programmer requirement.
Git allow you to revert files back to a previous state , revert the entire project back to a previous state , and also compare changes over time , you also able to see who last modified the code in file or any changes.

Here i write some most important and basic command of Git for Beginner Git User.

1.  How to install Git in Ubuntu  ?
--------------------------------------------------------------
/$ sudo apt-get update
/$ sudo apt-get install git
--------------------------------------------------------------

2. How to Set Up Git ?
--------------------------------------------------------------

/$ git config --global user.name "Your Name"
/$ git config --global user.email "youremail@domain.com"

----------------------------------For see all Configuration--------------------
/$ git config --list
-------------------------------------------------------------------------------------------------


Now this is some Basic Git Commands .


a) For create a new local repository
 =>  git init

b) Create a working copy of a local repository
 =>    git clone /path/to/repository

C) For  a remote server, use  
 => git clone username@host: /path/to/repositroy    

d) Add files (one or more files)
=> git add <filename>

e) Commit directory for local changes.
=> git commit -m "commit message"

f) Send changes to master branch of your remote repositor.
=> git push origin master

g) List the file you've changed in local branch and those you still need to add or commit.
=> git status

h) If you haven't connected you local repository to a remote server , then use this command to add the server to be able to push to it.
=> git remote add origin <server>

i) List all currently configured remote repositories:
=> git remote -v

 *Switch from one branch to another:
=> git checkout <branchname>

*For create a new branch.
=> git checkout -b <branchname>

*For check you current branch
=> git branch

*For delete the branch
=> git branch -d <branchname>

*Push all branches to your remote repository.
=> git push --all origin

j) Update your remote repository.
=> git pull


===================================
This is some use full and very basic command of git .
















Friday, 26 December 2014

Example of Single Table Inheritance in Rails 4



Single-table inheritance (STI) is the practice of storing multiple types of values in the same table, where each record includes a field indicating its :type , and the table includes a column for every field of all the types it stores. In Rails, the type column is used to determine which type of model to instantiate for each row; a row with type = 'Article' will make Rails call Article.new when turning that row into an object.

create_table 'employees' do |t|
  t.string  'type'
  t.string  'name'
  t.text    'email'
end

When we went with this design in the first place. After all, articles, videos, discussions and quizzes don’t seem like subtypes of one another. All these types of content have to be put into a sequence to make a course. So, each content item belongs to a course, and we use acts_as_list to keep the items, or ‘steps’, in order. This is most easily realised if all the steps live in the same table.

class Course < ActiveRecord::Base
  has_many :steps
end

class Step < ActiveRecord::Base
  belongs_to :course
  acts_as_list scope: :course
end

Schema of this model

create_table 'courses' do |t|
  t.string 'title'
end

create_table 'steps' do |t|
  t.string  'type'
  t.integer 'course_id'
  t.integer 'position',   null: false
  t.string  'title'
  t.text    'body'
  t.integer 'asset_id'
  t.string  'url'
  t.string  'copyright'
end

Then we have the content types themselves, for example Article hasbody and copyright attributes, Video has asset_id and copyright, andExercise has body and url.
class Article < Step
  validates :body,      presence: true
  validates :copyright, length: { maximum: 255 }
end

class Video < Step
  validates :asset_id,  presence: true
  validates :copyright, length: { maximum: 255 }
end

class Exercise < Step
  validates :body, :url, presence: true
end


STI should be used if your submodels will share the same attributes but need different behavior. If you plan to add 10 columns only used by one submodel, using different tables might be a better solution.

Friday, 19 December 2014

About Helper Method in Rails 4

The method helper_method is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and views . Like suppose we use application_controller.rb for example we add a method by name 
current_user like this

#application_controller.rb
helper_method :current_account
private
def current_account
#logged_user is method which return logged user find_by it's :session_id
  @current_account ||= logged_user.account if logged_user
  @current_account
end 

Now here helper_method create a method bye name of current_account and this is share for all controller which are inherited by #application_controller.rb. 

NOTE : helper_method not accessible in model.rb . Helper method use in controller only for view and if we write a method in xxxx_helper.rb then this method is only accessible in xxxx_controller.rb like  if we write code in account_helper.rb   it's only accessible in account_controller.rb.


Stripe for PAYMENT in Ruby on Rails application



Stripe is a company that provides a way for individuals and businesses to accept payments over the Internet.


Easy to implement a payment gateway by Stripe in Ruby on Rails Application
It's Provide many function for Client like

1.Create , Update ,Delete --> charges 
2.Create , Update ,Delete --> subscription
3.Create , Update ,Delete --> plain
4.Create , Update ,Delete --> coupons , customer,refund, discounts 


Step 1: adding Gem gem in Gemfile

gem 'stripe',  :git => 'https://github.com/stripe/stripe-ruby'

Step 2: Run 'bundle'
Step 3 : rails generate controller subscriptions

app/controller/subscriptions_controller.rb

def create 
  customer = Stripe::Customer.create(
      :card => params[:stripeToken],
      :plan => 'planname',
      :email => params[:stripeEmail],
  )

rescue Stripe::CardError => e flash[:error] = e.message
 redirect_to charges_path
end
step :4
add in config/route.rb
resources :subscriptions
step: 5 

config/initializers/stripe.rb:

Rails.configuration.stripe = {
 :publishable_key => 'PUBLISHABLE_KEY',
 :secret_key => 'SECRET_KEY'
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]




Step :6



Now let’s create new.html.erb under app/views/subscriptions, which is going to be our checkout page, which will display a credit card overlay which includes validation and error handling.
 


<%= form_tag subscriptions_path, :class => 'subscription_form' do %> 



<script src="https://checkout.stripe.com/checkout.js"


class="stripe-button"


data-key="<%= Rails.configuration.stripe[:publishable_key] %>"


data-description="Description about palin" 


data-amount="Plain amount" data-label='Pay' >


</script>



step :7


Finally let’s make a create.html.erb view under app/views/subscriptions that shows users a success message.


<h2>Thanks, you paid <strong>$5.00</strong>!</h2>








Friday, 5 December 2014

Difference Between Gem and Rails Engine

An Engine in rails terminology is a actually a sub-application of a web-application. For instance, something like a blog, a forum, or simple authentication: these are not full-blown applications, but pages/views/controllers/models that can be added to any rails application.
 gem is a ruby library, which can be found on http://rubygems.org and it is the standard (only) way to package and distribute ruby code to other ruby programmer. 
So we say:
  • A gem: is a generic library, which can be easily installed, which are version-managed, have dependencies and such.
  • An engine: is a sub-application of a Rails application, and since Rails 3 these are distributed as a gem.
So when will you use one or the other:
  • create a gem if you want to share ruby-functionality
  • create an engine (and package it in a gem) if you have parts of your rails application that can be used more generally.

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...