Routing error with Rails 3 with members

主宰稳场 提交于 2020-01-04 02:49:27

问题


I have the following route in rails 3:

resources :jobs do 
    member do 
      post :seller_job_submitted
    end
  end

And the following form

=form_for job, :url=>seller_job_submitted_job_path(job), :remote=>true do |f|

I know it's not very restful, but it's kind of a stop gap for now. In any case, I keep getting this error when submitting the form

Started POST "/jobs/74/seller_job_submitted" for 127.0.0.1    
ActionController::RoutingError (No route matches "/jobs/74/seller_job_submitted"):

but when I run rake routes | grep seller_job_submitted, I think the correct results come up:

seller_job_submitted_job POST   /jobs/:id/seller_job_submitted(.:format)                  {:action=>"seller_job_submitted", :controller=>"jobs"}

Any ideas about what might be going on?

Thanks!


回答1:


Assuming you have defined method seller_job_submitted in model and controller. Replace your code with

resources :jobs    
match "jobs/:id/seller_job_submitted" => "jobs#seller_job_submitted", :as => "seller_job_submitted"

Then in form_for tag use :url=>seller_job_submitted_path

This should fix your problem: you did not define seller_job_submitted_job_path explicitly.




回答2:


Perhaps use put instead of post? Or use :post as the method in the submit form.

You can tell if this is the issue by looking at what the REST method is for the generated form (look for the hidden field in the page source).

So in short, maybe Rails is somehow expecting a POST on that URL but it's receiving a PUT.




回答3:


Yes, this is a regression bug with Rails 3. It turns out you need to be careful about using POST in your routes.rb.

resources :jobs do
   member do
   post :seller_job_submitted # will not work
   put  :seller_job_submitted # will just work
end

This is even though the FORM method says POST.



来源:https://stackoverflow.com/questions/4868081/routing-error-with-rails-3-with-members

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!