开发者

Routing error with Rails 3 with members

开发者 https://www.devze.com 2023-02-07 19:36 出处:网络
I have the following route in rails 3: resources :jobs do member do post :seller_job_submitted end end And the following form

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_开发者_Python百科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!


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.


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.


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.

0

精彩评论

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

关注公众号