Ravindra
Ravindra

Reputation: 1049

Hw do i render a partial in rails 3.0 using ajax?

I want to use ajax in my application here is my problem :

I have a income voucher controller and model which receives incomes from various sources. for this i have a payment_mode model with card, cheque and internet_banking payment option here is my code:

From model: income_voucher

 class IncomeVoucher < ActiveRecord::Base
  has_one :payment_mode, :foreign_key => :voucher_id
 end

** payment_mode:**

class PaymentMode < ActiveRecord::Base
 belongs_to :transactionable, :polymorphic => true
 belongs_to :receipt_voucher    
end

card_payment:

class CardPayment < ActiveRecord::Base
  has_one :payment_mode, :as => :transactionable, :dependent => :destroy
end

similar in cheque and Internet banking model .

My controller: income_vouchers_controller:

class IncomeVouchersController < ApplicationController
def new
    @income_voucher = IncomeVoucher.new
    @invoices = current_company.invoices
    @income_voucher.build_payment_mode

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @income_voucher }
    end
  end
 def create
    @income_voucher = IncomeVoucher.new(params[:income_voucher])

   transaction_type = params[:transaction_type]
    payment_mode = nil
    if transaction_type == 'cheque'
      payment = ChequePayment.new(params[:cheque_payment])
    payment.amount = @income_voucher.amount
    elsif transaction_type == 'card'
      payment = CardPayment.new(params[:card_payment])
    payment.amount = @income_voucher.amount
    elsif transaction_type == 'ibank'
      payment = InternetBankingPayment.new(params[:internet_banking_payment])
    payment.amount = @income_voucher.amount
    else
      payment = CashPayment.new
    payment.amount = @income_voucher.amount
    end
    payment_mode = PaymentMode.new
    payment_mode.transactionable = payment

    @income_voucher.payment_mode = payment_mode
    respond_to do |format|
      if @income_voucher.save

        format.html { redirect_to(@income_voucher, :notice => 'Income voucher was successfully created.') }
        format.xml  { render :xml => @income_voucher, :status => :created, :location => @income_voucher }
      else

        format.html { render :action => "new" }
        format.xml  { render :xml => @income_voucher.errors, :status => :unprocessable_entity }
      end
    end
  end

In my form i did this:

<%= render :partial => "card_payment" %>
<%= render :partial => "cheque_payment" %>
<%= render :partial => "internet_banking_payment" %>

friend till now i am rendering my partials simply as we do in rails but now i want to do this using ajax. I hope you guy's have done this earlier. thanks

Upvotes: 0

Views: 327

Answers (1)

dmr
dmr

Reputation: 320

It's simple:

In your javascript (on page, for example:

$.ajax({
  url: "your_path",
  data: {  //params if needed
    your_param_name: param,
    your_param_name2: param2
  }
});

In your routes:

match 'your_path' => 'y_controller#y_method'

In y_controller:

def y_method
  # do smth with params[:your_param_name] if needed
  respond_to do |format|
    format.js
  end
end

In your y_method.js.erb:

$('#your-div').html('<%= raw escape_javascript render("cart_payment") %>'); //instead html() may be append()

Upvotes: 2

Related Questions