通常在rails中,将POST发送到RESTful控制器(例如/orders
)会调用#create
操作。这就是我想要发生的事情。而是调用#index
方法。我该如何解决这个问题?
服务器从POST登录到/orders
:
Started POST "/orders" for 173.8.132.62 at 2013-03-24 14:45:23 -0700
Processing by OrdersController#index as 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".* FROM "orders"
Rendered text template (0.0ms)
Completed 200 OK in 5ms (Views: 1.1ms | ActiveRecord: 1.2ms)
整个routes.rb
:
Wines::Application.routes.draw do
match ':controller(/:action(/:id))(.:format)'
resources :customers do
resources :phones
resources :addresses
end
resources :distributors do
resources :distributor_phones
resources :distributor_addresses
end
resources :items do
post 'sample', :on => :member
end
resources :orders do
post 'place', :on => :member
post 'deliver', :on => :member
resources :order_lines
end
resources :db do
post 'query', :on => :collection
end
resources :images, :beverage_types, :grapes
end
来自rake routes
的一些可能相关的线路:
place_order POST /orders/:id/place(.:format) orders#place
deliver_order POST /orders/:id/deliver(.:format) orders#deliver
order_order_lines GET /orders/:order_id/order_lines(.:format) order_lines#index
POST /orders/:order_id/order_lines(.:format) order_lines#create
new_order_order_line GET /orders/:order_id/order_lines/new(.:format) order_lines#new
edit_order_order_line GET /orders/:order_id/order_lines/:id/edit(.:format) order_lines#edit
order_order_line GET /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 GET /orders(.:format) orders#index
POST /orders(.:format) orders#create
new_order GET /orders/new(.:format) orders#new
edit_order GET /orders/:id/edit(.:format) orders#edit
order GET /orders/:id(.:format) orders#show
PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
我正在使用formtastic,而我的_form.html.erb看起来像这样:
<%= semantic_form_for(@order) do |f| %>
<% if @order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.semantic_fields_for :order_lines do |builder| %>
<%= render 'order_line_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add Order Line", f, :order_lines %>
<%= f.inputs :customer %>
<%= f.actions %>
<% end %>
生成的html <form>
标记如下所示:
<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">
生成的提交操作如下所示:
<input name="commit" type="submit" value="Create Order" />
控制器的相关代码如下。当我向/orders
提交POST时,我得到了
这是索引方法,使用URL
http://w:3000/orders
和 方法POST。
def index
@orders = Order.all
respond_to do |format|
format.xml # index.xml.erb
format.html {render :text => "this is the index method, called with URL #{request.url} and method #{request.method}"}
end
end
# GET /orders/1.xml
def show
@order = Order.find(params[:id])
respond_to do |format|
format.xml { render xml: @order.to_xml }
end
end
# GET /orders/new.xml
def new
@order = Order.new
1.times {@order.order_lines.build}
respond_to do |format|
format.xml { render xml: @order.to_xml }
format.html {}
end
end
# POST /orders.xml
def create
@order = Order.new(params[:order])
respond_to do |format|
if @order.save
format.xml { render xml: @order.to_noko_doc }
format.html { redirect_to @order, notice: 'Order was successfully created.'}
else
format.xml { render xml: @order.errors }
format.html {render action: 'new'}
end
end
答案 0 :(得分:2)
您的match ':controller(/:action(/:id))(.:format)'
路线首先与POST
到orders#create
匹配,将其移至路线文件的底部
答案 1 :(得分:1)
路由按照定义的顺序进行匹配,因此如果您的索引路由/orders
位于创建路由/orders :via => [:post]
之前,则会调用前者。尝试先放置您的邮寄路线。
match "orders" => "orders#create", :via => [:post]
# then resources :orders