0

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

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks @Alexeev Mikhail i did this but i have three partials which i want to render on radio button select
Will you please please tell me what changes do i need in my _form.html.erb file
If in your js method, which contains your ajax, get selected radio and send that as param data: { type: $('form input[type=radio]:checked').val() } in your view switch type-param: <% case params[:type] %> <% when '1' %> <%= render ... %> ... <% end %>
i have done this but nothing happened and in console server i got a message " Processing by IncomeVouchersController#income_voucher_partial as Parameters: {"$('form in{"type"=>"radio]:checked').val()=cash"}, "type"=>"cash"}", but nothing render
Thanks my console error has been solved, but no form partial renders yet here is my get message "Started GET "/income_vouchers/income_voucher_partial?$(%27form%20input[type=radio]:checked%27).val()=cash&type=cash""
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.