2012-03-14 58 views
0

在Rails 2.3.8中,我有一個需要customer_id的信用卡控制器。我使用before_filter和一個方法來獲取客戶ID。我使用像new_admin_credit_card_path(:customer_id => @ customer.id)的路徑到達信用卡控制器處理的頁面。我在提交創建或修改信用卡表單時遇到問題。客戶ID要麼沒有通過或沒有通過,但行動沒有正確迴應。這裏就是我想在我的form_for:提交表單時無法將參數傳遞給控制器​​

<% form_for :credit_card, 
:url => admin_credit_cards_path(:customer_id => @customer.id) do |f| %> 

    ...BLAH BLAH CODE BLAH... 

    <%= f.submit %> 

<% end %> 

這裏的錯誤,我得到:

路由錯誤 admin_credit_card_url未能從{產生:CUSTOMER_ID => 37165,:控制器=>「管理/ credit_cards 「::action =>」show「},expected:{:controller =>」admin/credit_cards「,:action =>」show「},diff:{:customer_id => 37165}

我也試試這個:

<% form_for (:credit_card, @credit_card, :url => { :controller => "admin/credit_cards", 
:action => "update" }) do |f| %> 

我也得到

未知的動作

沒有行動迴應37762.

它認爲客戶ID是行動。

這裏是我創建並在控制器更新方法:

def create 
    @credit_card = scope.new(params[:credit_card]) 
    set_modified @credit_card 

respond_to do |format| 
    if @credit_card.save 
    flash[:notice] = 'CreditCard was successfully created.' 
    format.html { redirect_to admin_credit_card_path(:customer_id => @customer.id) } 
    format.xml { head :created, :location => admin_credit_card_url(:customer_id =>  
@customer.id) } 
    else 
    format.html { render :action => "new" } 
    format.xml { render :xml => @credit_card.errors.to_xml } 
    end 
    end 
end 


def update 
@credit_card = scope.find(params[:id]) 
set_modified @credit_card 

respond_to do |format| 
    if @credit_card.save 
    flash[:notice] = 'CreditCard was successfully updated.' 
    format.html { redirect_to admin_credit_card_path(:customer_id => @customer.id) } 
    format.xml { head :ok } 
    else 
    format.html { render :action => "edit" } 
    format.xml { render :xml => @credit_card.errors.to_xml } 
    end 
    end 
end 

回答

1

您應該通過CUSTOMER_ID作爲隱藏字段的形式,而不是作爲路徑助手的一部分。

+0

好吧,我試過了,我得到這個錯誤:的ActiveRecord :: RecordNotFound在管理/信貸cardsController#創建 沒有ID 儘管CUSTOMER_ID的參數是無法找到的客戶: 請求 參數: {「format」=>「html」, 「commit」=>「保存更改」, 「credit_card」=> {「issuer_id」=>「1」, 「exp_year」=>「2023」, 「ct_card_number」=>「4111111111111111」, 「city」=>「New York」, 「state」=>「NY」, 「zip」=>「10016」, 「address1」=>「123 1st Ave」, 「address2」=>「」, 「exp_month」=>「1」, 「country」=>「840」 , 「customer_id」=>「37165」, 「holder_name」=>「NEW CARD」, 「active」=>「1」}} – 2012-03-15 16:24:52

+0

已修復。隱藏的領域不應該在形式上。它應該是一個hidden_​​field_tag – 2012-03-15 18:04:43

相關問題