Ruby on Rails - Map URL to a controller, method, and parameters - ruby-on-rails

I know that the file routes.rb in a Rails application maps a URL to a controller, a method, and a set of parameters in the params hash. This is complicated somewhat by "resources", nested resources, and non-RESTful routes.
Is there a way to run a command on the rails console to map a URL and figure out exactly which controller, which method, which http method(s), and the exact value of a parameter hash from a given URL?
Is it then possible to then run the controller method with the parameters hash on the rails console and get the output of the controller sent to STDOUT? If so, how?

Try these
Rails.application.routes.named_routes.each{|p,s| puts p,s}
Rails.application.routes.url_helpers.my_path_helper # To get url for a path helper
or
route = Rails.application.routes
route.recognize_path "/poweruser/3" # will give you result exactly you wanted

try it with running
bundle exec rake routes
See Determine if path exists as route in Rails controller
Rails.application.routes.recognize_path

Related

Rails display routes helper prefix

I'm trying to get the route prefix for the current page that a rails application is on. I know that you can get the controller & action info, and you can also get the path with:
request.env['PATH_INFO']
But there doesn't seem to be an environment variable for the prefix, that or i've missed it somewhere along the line. Is this possible, or do you have to find it through some hacky way using the controller & action / route?
it's almost as if i'm after a:
request.env['PATH_PREFIX']
or a:
get_prefix(controller_name, action_name)
SOLVED (In slim):
- Rails.application.routes.router.recognize(request) do |route, matches, param|
=> route.name

Rails - Find out where a url-helper points

In rails, when I have a form with a path-helper, for instance create_admin_shop_voucher_path and I want to find out what controller action it points to, I look in routes.rb:
rake routes | grep 'create_admin_shop_voucher'
But how do I do the same for a url-helper, for instance passed_url ?
Lets say you have this route in your RAILS_ROOT/config/routes.rb file
get "passed" => "passed#index", :as => :passed
If you call passed_url
rails will return the whole url. For instance http://localhost:3000/passed
if you call passed_path
rails will return the relative path /passed
*_url helper generates a URL that includes the protocol and host
name. The *_path helper generates only the path portion.
So, the its the same route in your RAILS_ROOT/config/routes.rb file
In your case you can call create_admin_shop_voucher_url as well as create_admin_shop_voucher_path.
Mostly you should use the _path flavor. If you need to spec the host or
protocol (like for talking to another app or service), then use the _url
flavor.
I use _url when i want to pass for instance a subdomain to a link.

Rails: Map of path helper methods to URL formats?

rake routes gives me a mapping of "controllerName#method" to URL path format
Rails.application.routes.named_routes.helpers gives me a list of all helper methods.
Is there something that can give me a mapping of all helper methods to URL path formats? I'm not very experienced with Rails so something that tells me what URL the Rails helper methods actually generate would be EXTREMELEY awesome.
I'm confused what you're asking? I think rake routes already tells you what you're looking for. Entries are in the format:
route_name HTTP_VERB path controller#action
Appending _path or _url to the route_name will give you a url helper. Ex. in my app I have
scenario GET /scenarios/:id(.:format) scenarios#show
And I can get a path or url to it by using scenario_path(scenario) or scenario_url(scenario) respectively.

No Route Matches for a very basic Rails4 App

I'm very new to RoR and I'm trying to get a very basic site going. I have my home page working okay, but when I try to add a new page, I keep getting "No route matches" in the log. Here is the output from rake routes:
Prefix Verb URI Pattern Controller#Action
inventing_index GET /inventing/index(.:format) inventing#index
ideas_index GET /ideas/index(.:format) ideas#index
root GET / ideas#index
However, when I go to mysite.com/inventing or mysite.com/inventing/index I get the no route matches error. mysite.com/ shows the app/views/ideas.erb as hoped. All I did was rails generate controller inventing index. Is there something else I have to do to activate the route?
I'm running ruby 2.0.0p247 and rails 4.0.0 with passenger/apache on centos 6. I installed all the ruby/rails/passenger stuff, so its possible something isn't setup properly.
Thanks
EDIT: Here is my routes.db file:
Rortest::Application.routes.draw do
get "inventing/index"
get "ideas/index"
root to: 'ideas#index'
end
tldr: Your problem is probably the route. Change get 'inventing/index' to get 'inventing/index'=> 'inventings#index' to correctly point the route to your controller action.
There are four things you need to do when adding a new page/route. I usually do them in this order for static pages:
1) Create a controller with the appropriate action for each page
You already did this with rails generate controller inventing index, but make sure that:
In your app/controllers folder, you do indeed have a file called inventings_controller.rb
In inventings_controller.rb, you have at least this:
class InventingsController < ApplicationController
def index
end
end
2) Create a view for each controller action
Make sure that:
In your app/views/inventings folder, you have a file called index.html.erb
Puts some simple HTML in that file, like <h1>Testing123</h1>, just so you can see if it's working.
3) Add a route for each controller action
It looks like this may be the problem. In your routes.rb file, you'll need this:
get 'inventing/index' => 'inventings#index'
Your root to works because it's actually pointing directly to your controller action, but rails isn't smart enough to guess that inventing/index should go to the index action in your inventing**s** controller.
4) As others have suggested, restart your rails app
You can do this with ctrl+c at the command line, then rails s again to start it back up.

Routing Error Ruby on Rails

I've installed ruby on rails 3.2.6, and when I execute
rails server
and access to 127.0.0.1:3000 it works, however when I generate a controller, for example
rails generate controller principal
and access to 127.0.0.1:3000/somecontroller, browser show following error:
Routing Error
No route matches [GET] "/principal"
Try running rake routes for more information on available routes.
What do I need to do, and can this be simply explained?
The problem is you did not specify any actions, so your controller 'principal' is empty, no views will be created with similar names, and no routes created.
You need to do:
rails generate controller principal index [show] [edit] [update] [create] [destroy]
The name after your controller name are the action names. Since you said controller 'principal' in the singular, then it might imply that you have a singular resource. If you want to have it in the plural, make sure you say 'controller principals'.
And your routes should should show:
resource :principal [ or :principals or multiple Restful routes ]
You need to edit config/routes.rb to tell the router which controller to route your request to. The rails standard is to use RESTful routes and in the example you've given this would equate to a singular Principal resource. Therefore you'd need to add:
resource :principal
to generate a set of RESTful routes for this resource. You can see the routes generated by doing:
rake routes
If you don't care about REST then you can simply add (assuming the PrincipalController has an index method):
match 'principal' => 'principal_controller#index'
Have a look at this chapter from the Rails Guides for more info on routing:
http://guides.rubyonrails.org/routing.html
You can also generate methods in the controller and routes at the same time by supplying their names as arguments to the rails generate controller command for example:
rails generate controller principal index

Resources