Recognize routes in rails console Session - ruby-on-rails

Say I have a router helper that I want more info on, like blogs_path, how do I find out the map statements behind that in console.
I tried generate and recognize and I got unrecognized method error, even after I did require 'config/routes.rb'

There is a good summary with examples at Zobie's Blog showing how to manually check URL-to-controller/action mapping and the converse. For example, start with
r = Rails.application.routes
to access the routes object (Zobie's page, a couple years old, says to use ActionController::Routing::Routes, but that's now deprecated in favor of Rails.application.routes). You can then check the routing based on a URL:
>> r.recognize_path "/station/index/42.html"
=> {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}
and see what URL is generated for a given controller/action/parameters combination:
>> r.generate :controller => :station, :action=> :index, :id=>42
=> /station/index/42
Thanks, Zobie!

In the console of a Rails 3.2 app:
# include routing and URL helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers
# use routes normally
users_path #=> "/users"

Basically(if I understood your question right) it boils down to including the UrlWriter Module:
include ActionController::UrlWriter
root_path
=> "/"
Or you can prepend app to the calls in the console e.g.:
ruby-1.9.2-p136 :002 > app.root_path
=> "/"
(This is all Rails v. 3.0.3)

running the routes command from your project directory will display your routing:
rake routes
is this what you had in mind?

If you are seeing errors like
ActionController::RoutingError: No route matches
Where it should be working, you may be using a rails gem or engine that does something like Spree does where it prepends routes, you may need to do something else to view routes in console.
In spree's case, this is in the routes file
Spree::Core::Engine.routes.prepend do
...
end
And to work like #mike-blythe suggests, you would then do this before generate or recognize_path.
r = Spree::Core::Engine.routes

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

Rails 3.2 Routing Error

In my first approach with Rails I have simply create a void SayController and static hello.rhtml view but when the page http://localhost:3000/say/hello started return me a Routing Error like this:
No route matches [GET] "/say/hello"
Try running rake routes for more information on available routes.
Rails version: 3.2.6
Seems like you didn't add a route for hello to your config/routes.rb file.
YourApp::Application.routes.draw do
match 'say/hello' => 'say#hello', :as => :hello
end
This will match route say/hello to controller say (the part before #) and action hello (the part after #).
:as => :hello makes it a named route so you can refer to it as hello_path from within your app.
The error message tells you to run rake routes (from the console) which will show you the existing routes in your app.
You should have something in your config/routes.rb to define that route. Try:
match 'say/hello' => 'say#hello', :as => 'say_hello'
The go to localhost:3000/say/hello
Also check out this documentation:
http://guides.rubyonrails.org/routing.html
I assume, controller: say and action: hello
Add following to config/route.rb
get 'say/hello' => 'Say#hello'

scope in routes.rb not properly working in connection with get

In my rails 3.2.2 app I have the following in my routes.rb:
scope "abc" do
get "hello/index"
end
Which should link "/abc/hello/index" to my index-action in my hello-controller, right?
Instead, I get the error "uninitialized constant Abc"
If I change it to the following
scope "abc" do
match "hello/index", to: "hello#index", via: :get
end
it works just fine.
From my understanding of the routing engine, the two should be the same, shouldn't they?
(See e.g.: http://guides.rubyonrails.org/routing.html#http-verb-constraints )
Also, if you do a "rails g controller hello index" a route named
get "hello/index"
is autocreated suggesting that this is the standard way of doing a non-restful get route.
So why can't I scope such a route? Any ideas?
The examples are using the notation scope "/abc", maybe the initial / is required.

Splitting Routes File Into Multiple Files

I'm working w/ a Rails 3 application and I want to split up the routes into separate files depending on the subdomain. Right now I have this in my routes.rb file:
Skateparks::Application.routes.draw do
constraints(:subdomain => 'api') do
load 'routes/api.rb'
end
end
And In my routes/api.rb file I have:
resources :skateparks
This doesn't seem to work though because if I run rake routes I get
undefined method `resources' for main:Object
Also, if I try to navigate to http://0.0.0.0:3000/ I get:
Routing Error
No route matches "/"
In Rails 3.2, config.paths is now a hash, so #sunkencity's solution can be modified to:
# config/application.rb
config.paths["config/routes"] << File.join(Rails.root, "config/routes/fooroutes.rb")
Sunkencity's answer seems to be identical to the following link, but for completeness' sake: https://rails-bestpractices.com/posts/2011/05/04/split-route-namespaces-into-different-files/
Note that routes defined later will override routes defined earlier. However, if you use something like
config.paths.config.routes.concat(
Dir[Rails.root.join('config/routes/*.rb')])
you don't know in what order the files will be read. So use
config.paths.config.routes.concat(
Dir[Rails.root.join('config/routes/*.rb')].sort)
instead, so you at least know they will be in alphabetical order.
Add the route file to the app route loading path:
# config/application.rb
config.paths.config.routes << File.join(Rails.root, "config/routes/fooroutes.rb")
Wrap your other route file in a block like this.
#config/routes/fooroutes.rb
Rails.application.routes.draw do |map|
match 'FOO' => 'foo/bar'
end
Works for me in rails 3.0
We used this in our app:
config.paths['config/routes'] = Dir["config/routes/*.rb"]
If you try to access config.paths['config/routes'] normally, it returns the relative path to config/routes.rb, so by doing the above you're giving it relative paths to all of the files in your routes folder and removing the reference to config/routes.rb

Sinatra & Rails 3 routes issue

I have just setup Sinatra v1.1.0 inside my rails (v3.0.1) app. But I can't invoke any routes that are more than 1 level deep, meaning this works - http://localhost/customer/3,
but this one does not work - http://localhost/customer/3/edit and I get a "Routing Error"
Here's the Sinatra object
class CustomerApp < Sinatra::Base
# this works
get "/customer/:id" do
"Hello Customer"
end
# this does NOT work
get "/customer/:id/edit" do
"Hello Customer"
end
end
This is what I have in my rails routes.rb file -
match '/customer/(:string)' => CustomerApp
I am guessing I need some magic in the routes file? What could be the problem?
In your routes file, you can specify the mapping this way:
mount CustomerApp, :at => '/customer'
Now, inside your sinatra application, you can specify your routes without the /customer part.
Dont't forget to require your sinatra application somewhere (you can do it directly in the route file)
You need to add an additional route to match the different URL:
match '/customer/(:string)/edit' => CustomerApp

Resources