I am trying to save a model named ‘customer’ using form_for tag. I do not have a controller for this model, i was hoping to use another controller ‘public’ for this task. So here is my view:
<%= form_for @customer do |f| %>
<div class="field">
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
.... and then
<%= f.submit 'Order', :action => :save_order %><br/>
and here is my controller
def check_out
@customer = Customer.new
end
def save_order
@customer = Customer.new(params[:customer])
credit_card_no = @customer.credit_card
@order = Order.new
@order.line_items << @cart.items
@customer.orders << @order
if @customer.save
# process credit card
@cart = nil
redirect_to(:action => :show_bill, :id => @order.id)
else
flash[:notice] = 'Could not process your credit card information'
render(:action => :check_out)
end
end
The view is loaded from action ‘check_out’ and it was supposed to go to action ‘save_order’ but i am getting an error in view for ‘form_for’ code, what am i doing wrong ? But if i create a controller or scaffold for ‘customer’ and try to use that, i get redirected to ‘customer/show/:id’ path, and i dont want that.
If you want to use different controller with form_for you need to use :url option in form_for.