1

通常在 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" />

控制器的相关代码如下。当我提交 POST 到 时/orders,我得到

这是索引方法,使用 URLhttp://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
4

2 回答 2

2

您的路线首先与tomatch ':controller(/:action(/:id))(.:format)'匹配,将其移动到路线文件的底部POSTorders#create

于 2013-03-25T18:29:04.007 回答
1

路由按照定义的顺序进行匹配,因此如果您的索引路由/orders在您的创建路由之前/orders :via => [:post],则会调用前者。尝试先放置您的发布路线。

match "orders" => "orders#create", :via => [:post]
# then resources :orders
于 2013-03-25T18:26:17.360 回答