2012-03-14 145 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

相关问题