Search code examples
ruby-on-railsrubyruby-on-rails-2

Trouble passing argument to controller while submitting a form


In Rails 2.3.8 I have a credit cards controller that needs a customer_id. I use a before_filter and a method to grab the customer id. I use paths like new_admin_credit_card_path(:customer_id => @customer.id) to get to pages handled by the credit card controller. I am having trouble on a form submit to create or edit credit cards. The customer id either doesn't get passed or gets passed but the action does not respond properly. Here's what I'm trying in my form_for:

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

  ...BLAH BLAH CODE BLAH...

  <%= f.submit %>

<% end %>

Here's the error I get:

Routing Error admin_credit_card_url failed to generate from {:customer_id=>37165, :controller=>"admin/credit_cards", :action=>"show"}, expected: {:controller=>"admin/credit_cards", :action=>"show"}, diff: {:customer_id=>37165}

I've also tried this:

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

And I get

Unknown action

No action responded to 37762.

It think the customer id is the action.

Here's my create and update methods in the controller:

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

Solution

  • You should pass the customer_id as a hidden field in the form, rather than as part of the path helper.