ruby - rails POST on /objects calling index method -
normally in rails, sending post restful controller, say, /orders
, calls #create
action. want happen. instead, #index
method gets called. how fix this?
the server log post /orders
:
started post "/orders" 173.8.132.62 @ 2013-03-24 14:45:23 -0700 processing orderscontroller#index html parameters: {"utf8"=>"✓", "authenticity_token"=>"ecsazxbyd5ovvo5oijzm/cnoyp7cz6drvbu7i41xeny=", "order"=>{"order_lines_attributes"=>{"0"=>{"item_id"=>"", "qty"=>"", "_destroy"=>"false"}}, "customer_id"=>""}, "commit"=>"create order"} order load (0.1ms) select "orders".* "orders" rendered text template (0.0ms) completed 200 ok in 5ms (views: 1.1ms | activerecord: 1.2ms)
the entire routes.rb
:
wines::application.routes.draw match ':controller(/:action(/:id))(.:format)' resources :customers resources :phones resources :addresses end resources :distributors resources :distributor_phones resources :distributor_addresses end resources :items post 'sample', :on => :member end resources :orders post 'place', :on => :member post 'deliver', :on => :member resources :order_lines end resources :db post 'query', :on => :collection end resources :images, :beverage_types, :grapes end
some possibly relevant lines routes rake routes
:
place_order post /orders/:id/place(.:format) orders#place deliver_order post /orders/:id/deliver(.:format) orders#deliver order_order_lines /orders/:order_id/order_lines(.:format) order_lines#index post /orders/:order_id/order_lines(.:format) order_lines#create new_order_order_line /orders/:order_id/order_lines/new(.:format) order_lines#new edit_order_order_line /orders/:order_id/order_lines/:id/edit(.:format) order_lines#edit order_order_line /orders/:order_id/order_lines/:id(.:format) order_lines#show put /orders/:order_id/order_lines/:id(.:format) order_lines#update delete /orders/:order_id/order_lines/:id(.:format) order_lines#destroy orders /orders(.:format) orders#index post /orders(.:format) orders#create new_order /orders/new(.:format) orders#new edit_order /orders/:id/edit(.:format) orders#edit order /orders/:id(.:format) orders#show put /orders/:id(.:format) orders#update delete /orders/:id(.:format) orders#destroy
i'm using formtastic, , _form.html.erb looks this:
<%= semantic_form_for(@order) |f| %> <% if @order.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@order.errors.count, "error") %> prohibited order being saved:</h2> <ul> <% @order.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <%= f.semantic_fields_for :order_lines |builder| %> <%= render 'order_line_fields', f: builder %> <% end %> <%= link_to_add_fields "add order line", f, :order_lines %> <%= f.inputs :customer %> <%= f.actions %> <% end %>
the generated html <form>
tag looks this:
<form accept-charset="utf-8" action="/orders" class="formtastic order" id="new_order" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline">
the generated submit action looks this:
<input name="commit" type="submit" value="create order" />
relevant code controller follows. when submit post /orders
, get
this index method, called url
http://w:3000/orders
, method post.
def index @orders = order.all respond_to |format| format.xml # index.xml.erb format.html {render :text => "this index method, called url #{request.url} , method #{request.method}"} end end # /orders/1.xml def show @order = order.find(params[:id]) respond_to |format| format.xml { render xml: @order.to_xml } end end # /orders/new.xml def new @order = order.new 1.times {@order.order_lines.build} respond_to |format| format.xml { render xml: @order.to_xml } format.html {} end end # post /orders.xml def create @order = order.new(params[:order]) respond_to |format| if @order.save format.xml { render xml: @order.to_noko_doc } format.html { redirect_to @order, notice: 'order created.'} else format.xml { render xml: @order.errors } format.html {render action: 'new'} end end
your match ':controller(/:action(/:id))(.:format)'
route getting matched first post
orders#create
, move bottom of routes file
Comments
Post a Comment