How can i find the path helper from specific route i have - ruby-on-rails

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.

Related

path helper generating exeception on console while migrating from Rails-2 to Rails-3

I am trying to modify my script, i am migrating from rails 2.3 to rails 3.1 but i am facing a strange issue. I see that when i use a path helper like this
in rails 3.1 i am getting an exception but rails 2.3 it used to work, by work i mean when to the path helper i pass values and order.customer_id is nil, it generates a path to create new customer however in rails 3.1 i see it generates a exception, below is the description of the error i see in rails 3.1
helper.link_to customer_email, app.store_customer_path(store,order.customer_id) , when order.customer_id is blank i get a exception on console as below
Below is the error i observe in rails 2.3
ActionController::RoutingError: store_customer_url failed to generate from {:controller=>"customers", :action=>"show", :store_id=#<object>}
. but when i load a web page i see that i get a path generated to create a new customer.
here is my relevant routes.rb code
resources :stores do
resources :customers do
collection do
get :get_customers, :download, :csv_template
end
match :upload, :import, :map, :on => :collection
member do
get :more
end
resources :dropship_profiles
resources :address
end
end
But in rails 3.1
on console to i see exception and when loaded from browser also i see exception
I am not able to understand this and its confusing me, can anyone please help, Thanks.
After going through your routes.rb,i dont see any relevant path that routes to store_customer_path...kindly use the one present or create new one...verify by running rake routes > path.txt and then check your routes easily in path.txt
=============UPDATED ANSWER==============
here you are passing two values,you just need to pass one
Instead of helper.link_to customer_email, app.store_customer_path(store,order.customer_id)
try
helper.link_to customer_email, app.store_customer_path(order.customer_id)
or
helper.link_to customer_email, app.store_customer_path(store.id)
As your path says /stores/:store_id/customers/new(.:format),you just need to pass store_id for it to work.
for understanding major changes in Routes from Rails 2 to Rails 3...you must have a look at this page

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

Getting no route matches on the show action

I'm trying to use the path from the following route, here is what it's like in rake routes
chapter GET /chapters/:id(.:format) {:action=>"show", :controller=>"chapters"}
chapter_path creates a link to /chapters/x which is correct but I get the routing error when trying to access it.
No route matches {:controller=>"chapters"}`
this is my routes (I am using shallow routing to create a books_chapters and book_chapters_new paths.
resources :books do
resources :chapters, :shallow => true
end
when I test the route with rake routes, I get books_chapters, books_chapters_new, chapters and books, so I don't know what's wrong.
when i remove :shallow => true, i can access /books/1/chapters/6 but I just want it to be /chapters/6
this is what my terminal looks like
so /chapters/id and /chapters/id/edit should be working fine.
I've restarted the server with touch tmp/restart.txt and ran rails s to see if the routes worked there too and rake routes is giving me acceptable routes, but they don't work for me.
Are you supplying the parameter for the path helper, something like
chapter_path(#chapter)
I couldn't figure out how to get :shallow routes to work, and there isn't an example on how to use :shallow in the rails guide so, instead I have to just use nested routes like so
resources :books do
resources :chapters
end
now this means the something like chapters_url or chapters_path won't work.
So I have to do something like this everywhere
book_chapter_url(#chapter.book, #chapter)
or
edit_book_chapter_path(#chapter.book, #chapter)
It works but there's a bit of code smell because I use #chapter twice and the whole url should be able to resolve just through the chapter id instead.

autogenerate paths in rails 3?

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

Routing questions with rails

I am just starting with rails and I have a very simple case. I have a "home" controller. There is one action 'index' defined in this controller. When I go to ~/home I get an error msg saying:
uninitialized constant HomesController (I noticed the singular/plural thing).
That's the first thing I don't get (I thought it would automatically go to ~/home/index).
Second thing, if I go to ~/home/edit (note that this action doesn't exist yet), I get also:
uninitialized constant HomesController
But if I go to ~/home/show (show doesn't exist as well) I get a different error message:
No route matches "/home/show"
How can I get 2 differents errors for the same reason (an inexistant action). And What's the deal with this constant?
Thank you
Edit
I'm running rails 3.0
Here is my routes.rb file
Topnotch::Application.routes.draw do
resources :subscriptions
resource :home
get "home/index"
get "subscriptions/index"
root :to => "home#index"
end
You must add the resource "home" to the route.rb.
The controllers are considered to be plural.
If you are new to rails, I suggest you to start using generators - just open a terminal in the project's folder and type in "script/generate scaffold home" (in rails3 it would be "rails g home")
Changes the root route as below:-
root :to => "homes#index".
You must use the plural form in the routes.
Turns out, the routes were correct I was just not using them correctly !
rake routes helped.

Resources