开发者

How to solve this routing error?

So for some reason, I'm getting "No route matches "/accounting/payment_objects/57/comments"" when trying to post to that url.

As you can see from rake routes, that url is routed.

Now here's something strange, the GET request works but the POST request doesn't.

Also I can use the _path helper method to generate the route.

accou开发者_StackOverflow中文版nting_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"}

But when I post to it, log returns this:

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)

I have confirmed this has nothing to do with my controller/actions.

But here's a block of code from my routes.rb that is relativent to this issue.

  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


This was happening because there are two buttons in your form.

One of them must have a method set to something else.

Example of a form that could cause this problem:

<%= 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 %>

If you change the button_to to a link_to or move it out of the form block, this should resolve any issues you are having.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜