autogenerate paths in rails 3? - ruby-on-rails

From the looks of some railscasts (this one in particular), it seems like there is some autogeneration of "*_path" variables that not happening for me. in this rails cast, the edit_mutliple_products_path seems to be generated automagically (i don't normally like using that word). When I follow through the same steps and try to access a similar path, i get this:
undefined local variable or method `edit_multiple_distributions_workflows_path' for #<#<Class:0x132b18a68>:0x132af3290>

This is rails 2.X. Rails routes changed in Rails 3. in order to get this route add below to routes.rb:
resources :products do
collection do
post 'edit_multiple'
put 'update_multiple'
end
end
You will be able to access this paths with
edit_multiple_products_url
edit_multiple_products_path
update_multiple_products_url
update_multiple_products_path
instead of edit_multiple_distributions_workflow_path. Btw where did you get this path from? I did not see it in the railscast.

In the given tutorial, which looks like it's from an older Rails, this is the line which would generate the path methods:
map.resources :products, :collection => { :edit_multiple => :post, :update_multiple => :put }
In rails 3, you can see its usage in the docs here: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

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.

How to get raw route of named helper?

I have
# routes.rb
Rails.application.routes.draw do
get 'order/:order_id/add_item/:item_id' => 'order#item_add', as: :order_add_item
end
I can call order_add_item_url(order_id: something, item_id: some_other) and it will return order/something/add_item/some_other.
My question is, can i get the "raw" URL? So i want to get something like order_add_item_url returns order/:order_id/add_item/:item_id
I have digged into Rails' Journey internals, and get field something like this:
Rails.application.routes.named_routes.routes[:order_add_item].path.source
=> "\\A/pos/order/([^/.?]+)/add_item/([^/.?]+)(?:\\.([^/.?]+))?\\Z"
But it strips down the "original" version of the path i wrote in the routes. There is no :item_id and :order_id
I am using Rails 4.2.
I believe this is what you want or at least somewhere for you to start working on:
Rails.application.routes.named_routes.routes[:order_add_item].path.spec.to_s

How can i find the path helper from specific route i have

I have a specific path helper that works perfectly in rails 2.3 but throws error on rails 3.1.
Here is the path helper.
shipping_price_store_return_path(store)
When i use this in rails 3.1 it gives me error saying
NoMethodError: undefined method `shipping_price_store_return_path' for #<ActionDispatch::Integration::Session:0x007fb2da730228>
when i run rake routes This is whati get shipping_price_store_return_index /stores/:store_id/return/shipping_price(.:format) {:action=>"shipping_price", :controller=>"return"}
can anyone suggest what could be going wrong here.
below are the content of routes file
resources :stores do
resources :return do
match :shipping_price, :on => :collection
end
end
As your resource name is :return instead of :returns that Rails decided to add the _index to any collection nested underneath. This change has been done from rails 3 onwards.
So the new rails 3 route should be:
shipping_price_store_return_index_path
If you want to avoid the _index then either you can use resources :returns or you can make it resource :return.

Migrating Routes to Rails 4

I am about to migrate my rails 3 application to rails 4.
There are some additional routes on my ressources that make some problems.
I get an error message for this lines in my route file:
resources :teams do
...
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :apply_membership
...
This is the generated error message
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
In rails3 it was possible to define a get and a post route using the same alias and routing them to different controler methods.
Can I do this in rails4, too?
And if yes, how does it have to look like in my route file?
You can not take two route name with same name. but you have done it. so please change,
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :update_membership
Take a look here for rails routings. http://guides.rubyonrails.org/routing.html

Adding a record's attribute to the URL before the ID

I'm trying to add a specific attribute of a record in Rails to the URL from something like:
domain.com/share/5
(where 5 is the record ID) to something like:
domain.com/share/johnsmith/5
where johnsmith is stored in record 5. I'm alternating between these two routes to no success:
get "/share/:name/:id" => "share#show"
resources :share, :only => [:show]
And between these two methods:
share_path(doc.user.name, doc)
share_path(doc)
The show method in the ShareController is pretty standard.
The problem:
Using share_path(doc.user.name, doc) in the view generates a link to /share/johnsmith.5, instead of /share/johnsmith/5.
get "/share/:name/:id" => "share#show" should do the job. But you may have to look at the order of routes in routes.rb, maybe Rails took the wrong route?
Best tip to look at what's happening:
Call the URL in your browser (or using curl or whatever) and then look into your console where your started rails s (or rails server).
There you should see something like this:
Processing by ShareController#show
Parameters: {"id"=>"5", "name"=>"johnsmith"}
Concerning the path methods:
Simply use rake routes, it will tell you which path methods are available.
No idea what happened but it resolved itself with this:
get "/share/:name/:id" => "share#show", :as => :share
share_path(doc.user.name, doc)
I do not get the . and / issue at all. I restarted everything and it was gone.

Resources