在提交表单时将参数传递给控制器​​时遇到问题

时间:2012-03-14 22:30:07

标签: ruby-on-rails ruby ruby-on-rails-2

在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 =&gt; 37165,:controller =&gt;“admin / credit_cards”,:action =&gt;“show”}生成,预期:{:controller =&gt;“admin / credit_cards”,:action =&gt;“show”},diff:{:customer_id =&gt; 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 个答案:

答案 0 :(得分:1)

您应该将customer_id作为表单中的隐藏字段传递,而不是作为路径助手的一部分。