如何解决这个路由错误?

时间:2011-07-22 06:57:41

标签: ruby-on-rails routing

因此,出于某种原因,我在尝试发布到该网址时收到“无路由匹配”/ accounting / payment_objects / 57 / comments“”。

正如您从rake路线中看到的那样,该路由被路由。

现在这里有点奇怪,GET请求有效但POST请求没有。

我也可以使用_path辅助方法生成路由。

accounting_payment_object_comments GET    /accounting/payment_objects/:payment_object_id/comments(.:format)                             {:action=>"index", :controller=>"accounting/comments"}
                                   POST   /accounting/payment_objects/:payment_object_id/comments(.:format)                             {:action=>"create", :controller=>"accounting/comments"}

但是当我发帖时,日志会返回:

Started POST "/accounting/payment_objects/57/comments" for 127.0.0.1 at Fri Jul 22 14:53:58 +0800 2011

ActionController::RoutingError (No route matches "/accounting/payment_objects/57/comments"):


Rendered /Users/pma/.rvm/gems/ree-1.8.7-2010.02/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)

我已经确认这与我的控制器/操作无关。

但是这里是我的routes.rb中的一段代码,与此问题相关。

  namespace 'accounting' do
    resource :requisitions, :only => [:show] do
      collection do
        get 'filter'
      end
    end
    resource :purchase_orders, :only => [:show]
    resource :accounts_payable, :only => [:show]
    resource :payment_requests, :only => [:show, :update]
    resource :general_ledger, :only => [:show]
    resource :permissions, :only => [:update]

    resources :payment_objects do
      collection do
        get 'filter'
        post 'permissions'
      end
      member do
        match 'approve'
        match 'decline'
      end
      resources :items do
        member do
          get 'receive' => 'items#received_form'
          post 'receive' => 'items#process_received_form'
        end
        resources :receiving_records, :only => [:create, :new]
      end
      resources :files
      resources :comments, :except => [:show, :new]
    end
  end

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为表单中有两个按钮。

其中一个必须将方法设置为其他内容。

可能导致此问题的表单示例:

<%= form_for @comment, :url => @create_url, :method => :post do |f| %>
  <%= f.text_area :content %>
  <div class="space inline">
    <%= f.submit %>
    <%= button_to 'Discard Draft', @discard_url, :id => 'discard_draft_link', :method => :delete %>
    <p id="draft_save_message">
      <% if @draft %>
        Draft was last saved at: <%= @draft.updated_at.getlocal %>
      <% else %>
        Draft not saved yet.
      <% end %>
    </p>
  </div>
<% end %>

如果您将button_to更改为link_to或将其移出表单块,则应解决您遇到的任何问题。