0

私のアプリケーションでajaxを使用したいのですが、これが私の問題です:

私は収入バウチャーコントローラーとさまざまなソースから収入を受け取るモデルを持っています。このために私はカード、小切手、internet_banking支払いオプションを備えたpayment_modeモデルを持っていますここに私のコードがあります:

モデルから: income_voucher

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

** 支払いモード:**

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

小切手とインターネットバンキングモデルで同様です。

私のコントローラー: 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

私の形で私はこれをしました:

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

これまでの友人は、レールの場合と同じようにパーシャルをレンダリングしていますが、今はajaxを使用してこれを実行したいと考えています。私はあなたたちがこれを早くやったことを望みます。ありがとう

4

1 に答える 1

2

それは簡単です:

あなたのJavaScriptで(ページ上、例えば:

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

あなたのルートで:

match 'your_path' => 'y_controller#y_method'

y_controller で:

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

y_method.js.erb で:

$('#your-div').html('<%= raw escape_javascript render("cart_payment") %>'); //instead html() may be append()
于 2012-05-29T11:04:39.623 に答える