fatfrog
fatfrog

Reputation: 2160

form_for going to the wrong URL

I'm on the order view page, and I want to update a line item on that order:

I have my route:

  resources :order_line_details do
    member do 
      put  :update_barcode
    end
  end

Then my form_for :

<%= form_for update_barcode_order_line_detail_path(@order_line_detail) do |f| %>
<%= f.text_field :hds_barcode  %>
<%= f.submit 'Submit' %>
<% end %>

When I submit though, it just goes to the order again:

No route matches "/orders/20888"

Rake routes:(for orders and orderlinedetails)

orders GET      /orders(.:format)                                              {:action=>"index", :controller=>"orders"}
                                  orders POST     /orders(.:format)                                              {:action=>"create", :controller=>"orders"}
                               new_order GET      /orders/new(.:format)                                          {:action=>"new", :controller=>"orders"}
                              edit_order GET      /orders/:id/edit(.:format)                                     {:action=>"edit", :controller=>"orders"}
                                   order GET      /orders/:id(.:format)                                          {:action=>"show", :controller=>"orders"}
                                   order PUT      /orders/:id(.:format)                                          {:action=>"update", :controller=>"orders"}
                                   order DELETE   /orders/:id(.:format)                                          {:action=>"destroy", :controller=>"orders"}


   update_barcode_order_line_detail PUT      /order_line_details/:id/update_barcode(.:format)               {:action=>"update_barcode", :controller=>"order_line_details"}
                      order_line_details GET      /order_line_details(.:format)                                  {:action=>"index", :controller=>"order_line_details"}
                      order_line_details POST     /order_line_details(.:format)                                  {:action=>"create", :controller=>"order_line_details"}
                   new_order_line_detail GET      /order_line_details/new(.:format)                              {:action=>"new", :controller=>"order_line_details"}
                  edit_order_line_detail GET      /order_line_details/:id/edit(.:format)                         {:action=>"edit", :controller=>"order_line_details"}
                       order_line_detail GET      /order_line_details/:id(.:format)                              {:action=>"show", :controller=>"order_line_details"}
                       order_line_detail PUT      /order_line_details/:id(.:format)                              {:action=>"update", :controller=>"order_line_details"}
                       order_line_detail DELETE   /order_line_details/:id(.:format)                              {:action=>"destroy", :controller=>"order_line_details"}

What am I doing wrong?

Upvotes: 3

Views: 4916

Answers (1)

DanneManne
DanneManne

Reputation: 21180

The first argument to the form_for method should be the object that the form is for. Then you can manually change the default url with the :url parameter. Try doing it like this instead:

<%= form_for @order_line_detail, :url => update_barcode_order_line_detail_path(@order_line_detail) do |f| %>
  <%= f.text_field :hds_barcode  %>
  <%= f.submit 'Submit' %>
<% end %>

Upvotes: 8

Related Questions