Routing error with Rails 3 with members - ruby-on-rails

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!

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.

Related

Ruby on Rails 'link_to' method creates wrong link

I am having issues with the link_to method in Rails. I have the routes established, but the urls aren't working correctly.
3000/gov_official => my root page
3000/gov_official/1 => desired show page url
3000/gov_official.1 => what I am getting...
Any help would be greatly appreciated.
My code snippet:
Try the following in your routes instead of manually defining the index/show routes.
resource: :gov_official, only: [:get]
Probably having an issue link_to, try this one.
<%= link_to gov.name, gov_official_path(id: gov.id) %>
From my own experience, the problem is that Rails can't figure out which path are you trying to link to. Because, your resource is not in plural form gov_officials, you don't have 2 clearly separate paths:
gov_officials_path - takes 1 argument format. Generates: gov_officials.format.
gov_official_path - takes 1 argument model. Generates: gov_officials/:id
So, to solve your problem the Rails way, use pluralization of your resources correctly.

Polymorphic Routes in Rails 4.1.4

I have a polymorphic association between Posts and Postables, right now with Projects being my only Postables. In my routes, I have:
resources :projects do
...
member do
resources :posts
end
end
I know how to retrieve the right ids from the parameters, and all of my controller specs pass just fine, but when I try to write links in my views, they don't work. Running rake routes, I see a little weirdness:
...
post SHOW /projects/:id/posts/:id(.:format) posts#edit
...
And so on for the rest. If I'm not mistaken, the path should be 'new_project_post', and the first parameter should be :project_id.
Now, in my show view for Projects, I have the index of posts for that particular project. But the links don't work. Lets assume I have a project with an ID of 2, and a post for that project with an ID of 1.
If I try link_to 'Edit', edit_post_path(#project, post), the link comes out as .../projects/1/posts/1/edit, so both :ids get the post's id. If I swap post and #project, both :ids will be the project's id.
If I try passing them as an array, link_to 'Edit', post_path([post, #project]), the resulting link will be .../projects/1%2F2/posts/1%2F2/edit. %2F is a URL-encoded slash character, so I'm not sure what the hell Rails is trying to do here.
If I try using polymorphic_path([#project, post]) for my links, all it does is spit out paths that don't exist: undefined method 'project_post_path'
I've tried several other combinations of parameters without success. So if anyone could point me in the right direction, I'd be extremely grateful.
The appropriate syntax for nested resources in Rails is:
resources :projects do
resources :posts
end
In member block you could only declare additional actions to work with project instances.
You are using nested resource inside member and it is incorrect. Read more about this here: http://thelazylog.com/posts/polymorphic-routes-in-rails

rails 4 nested resource routing error

I have a routes file (Rails 4.0.4 App):
resources :products do
resources :variants, except: :index
end
rake routes gives:
product_variants POST /products/:product_id/variants(.:format) variants#create
new_product_variant GET /products/:product_id/variants/new(.:format) variants#new
edit_product_variant GET /products/:product_id/variants/:id/edit(.:format) variants#edit
product_variant GET /products/:product_id/variants/:id(.:format) variants#show
PATCH /products/:product_id/variants/:id(.:format) variants#update
PUT /products/:product_id/variants/:id(.:format) variants#update
DELETE /products/:product_id/variants/:id(.:format) variants#destroy
So as proposed I'm using
<%= form_for [#product, #variant] do |f| %>
but this generates (note the URL):
<form accept-charset="UTF-8" action="/variants/1" class="edit_variant" id="edit_variant_1" method="post">
And I get (of course) the routing error saying no route matches with PATCH /variants/1
Same strange behaviour when I redirect to in an Api Controller living namespaced under api/:
redirect_to [:api, #product, #variant]
Got me: no route matches with /api/variants/1
So:
I checked all routes
I know how to nest resources and namespace them
I thought I know how to generate the URL's ;-)
Somehow the #products is ignored when generating the urls
Has anyone a clue where to look any further?
Take a look at your variants controller. Are you instantiating #product?
It may be helpful to post the code for controller too.
Hope this helps!
Alex
Okay the problem really lies in the different controllers. I somehow instantiated #products in a before action, but obviously did it wrong.
Seems to be a leck of energy drink problem. thanks for pointing me to the right solution!

Strange routing error in Rails 3.0.1

This irritates me right now, and I've been at it for more than an hour. Maybe one of you has an idea.
I've defined the following routes in routes.rb:
resources :ads do
member do
post :preview_revision
get :revise
end
collection do
get :search
post :preview
end
end
When I run rake routes, the preview_revision route is shown correctly:
preview_revision_ad POST /ads/:id/preview_revision(.:format) {:action=>"preview_revision", :controller=>"ads"}
However, when I make a POST request to /ads/145/preview_revision, I get the following error:
Started POST "/ads/145/preview_revision" for 127.0.0.1 at Mon Nov 15 17:45:51 +0100 2010
ActionController::RoutingError (No route matches "/ads/145/preview_revision"):
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.2ms)
The id (145) exists, yes.
The action and the controller (action: preview_revision, controller: ads) exist as well.
All the other routes work perfectly fine
I've tried restarting the server several times
I double-checked that it's actually a POST request, that the ID is correct, I really don't know what to do anymore.
Rails version is 3.0.1
--- UPDATE ---
I tried changing the method from POST to PUT, and now it works. Apparently POST routes on member level are not working, or not allowed.
Still, I want to know why. I do know that POST is for creating a member, so a POST request on a member makes no sense, but I didn't find this mentioned anywhere in the Rails guides, and why would rake routes display the route, if it doesn't actually work? Is that a bug?
--- UPDATE 2 ---
The Rails Routing guide (Edge version) specifically says:
2.9.1 Adding Member Routes
To add a member route, just add a
member block into the resource block:
resources :photos
do member do
get 'preview'
end
end
This will recognize /photos/1/preview
with GET, and route to the preview
action of PhotosController. It will
also create the preview_photo_url and
preview_photo_path helpers.
Within the block of member routes,
each route name specifies the HTTP
verb that it will recognize. You can
use get, put, post, or delete here. If
you don’t have multiple member routes,
you can also pass :on to a route,
eliminating the block [...]
So is the POST on member possible or not possible? Am I missing something? Just to be clear, I don't wanna use it anymore, but still, it bugs me not knowing why it doesn't work.
The best thing to do, if you find a bug in actively maintained open-source software, is:
to submit a bug report to the project's bug tracker isolating the problem as much as possible and with as much detail as possible, and, optionally,
to include a patch that fixes the bug if you can.
The bug tracker for Rails is at rails.lighthouseapp.com.
---edit---
The bug tracker for Rails is now at github.com/rails/rails/issues.
That should be working. I've created a blank app with 3.0.1 and added those routes and it worked.
Have you tried passing the :on param? Something like:
resources :ads do
post :preview_revision, :on => :member
get :revise, :on => :member
get :search, :on => :collection
post :preview, :on => :collection
end

Form Action garbled, Rails 2.3.3

I seem to have a problem that I can't find the solution for myself, I hope someone can help.
I have a form defined like so:
<% form_for #leads do |f| %>
I have a resource called #leads (map.resource :leads)
But when I look in the HTML code of the page it generates, I see as a form action the following
<form action="/lead.%23%3Clead:0x10333e858%3E" class="edit_lead" ... etc
The lead.%23%3Clead:0x10333e858%3E as a form action does work, however rails doesn't know what to do with it after it updates. Does anyone know how I can make this a normal URL so that rails can redirect after the update again?
Thank you very much
Regards, Marco
I think you have to rename your route from
map.resource :leads
to
map.resources :leads
because you have multiple leads (and not only one -> so no "resource", its "resources")
If you are using a singular resource you should not pass the object to the url helper, ie. lead_path not lead_path(#lead).
However it does look like a typo and your route should be map.resources :leads

Resources