rails 4 nested resource routing error - ruby-on-rails

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!

Related

Rails nested resources not working

I was trying to add Stripe to my Rails application following this tutorial, but couldn't figure out what went wrong:
Assigning Charges To Resources With Stripe Checkout
Below is my code,
routes.rb
resources :people, :path => "" do
member do
put :activate
put :deactivate
end
resources :listings do
member do
put :close
put :move_to_top
put :show_in_updates_email
end
resources :charges
end
and in the.haml
= form_tag listings_charges_path(#listings) do
but there is error like below:
undefined method `listings_charges_path' for #<#<Class:0x007f0690ea1788>:0x007f06b1864b88>
= form_tag listings_charges_path(#listings) do
Is it because the charges is nested inside listings resource, and listings is also nested in another one? It's weird cause it's totally working fine if I changed the code back to none nested resources.
= form_tag charges_path do
Any help is really appreciated.
Thanks!
This worked, but there is something new:
= form_tag person_listing_charges_path(#person, #listing, #charges) do
New error:
No route matches missing required keys:{:action=>"index", :controller=>"charges", :id=>"111-abc", :listing_id=>nil, :locale=>nil, :person_id=>#<Listing id: 111, ..........} [:listing_id]
Your path is people_listing_charge_path(#people, #listing, #charge) if you want to do this. Or maybe person_listing_charge_path(#person, #listing, #charge) depending on how Rails helper works. As you can see, it gets pretty cumbersome to handle.
If you check Rails guide on nested route, it suggests some ways to handle the nesting. I highly suggest you follow these suggestions.
You probably should do (if your people singular is person)
person_listing_charge_path(#person, #listing, #charge)
To know your routes, you can do rake routes
FYI: According to ruby on rails guides
Resources should never be nested more than 1 level deep.

Set Up Route for Accessing Private S3 Content

I've been following
https://github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3
and
Rails 3, paperclip + S3 - Howto Store for an Instance and Protect Access to try and get Paperclip's expiring links to work. I believe most of what I'm running into is one of the routing variety.
In my pieces_controller I put a method in like this
def download
redirect_to #asset.asset.expiring_url(1000)
end
And then in my routes, I put this:
match "pieces/download"
Then in my view I have:
<%= link_to download_asset_path(piece)%>
It would seem to be far from working, and I'm not sure what is messed up. I know I'm getting routing errors for one, but it's also telling me that my download_asset_path is undefined, which is likely also routing related... I feel like I'm doing everything all wrong.
Tearing my hair out. Thanks!
Try modifying your routes file to:
match 'pieces/download' => 'pieces#download', :as => 'download_asset'
Your match needs to tell which controller#action to go to, and the as option will allow you to name the route download_asset_path.
If your pieces controller is for a Piece resource it could be cleaner like:
resources :pieces do
member do
get :download
end
end
But then you would want to change the link to:
link_to 'Link text', download_piece_path(piece)
For further reading: http://guides.rubyonrails.org/routing.html

Making a controller exist on as a sub-controller, how to fix form routes in rails

I have a ChaptersController that does not have a direct route (i.e. site/chapters/:id) but only exists as a sub route for a BooksController (i.e. site/books/:id/chapters/:id). however, when I try to go to books/:id/chapters/new , I get the following routing error:
Showing .../app/views/chapters/_form.html.erb where line #1 raised:
No route matches {:controller=>"chapters"}
how can I fix this?
It seems like you are using nested routes in this manner:
resources :books do
resources :chapters
end
in which case you should have the named routes 'book_chapter' and 'book_chapters'. You can check this by running rake routes.
In your _form.html.erb partial you need to change this line:
<%= form_for(#chapter) do |f|%>
You need to specify the target URL of the form explicitly, and probably also handle different URLs for create and update scenarios. Try something like this:
<%= form_for(#chapter, :url => (#chapter.new_record? ? book_chapters_path(#book) : book_chapter_path(#book, #chapter) )) do |f| %>
I suppose there is wrong path in /app/views/chapters/_form.html.erb
Check what url is in tag. I suppose you forgot to change it to nested in books.
You may as well paste _form.html.erb here, so i will point it out :)

Routing error with Rails 3 with members

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.

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