Rails display routes helper prefix - ruby-on-rails

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

Related

Rails route 'stacking' (not sure what it's called)

What's the problem here?:
get 'cars/index' <- works
get 'carsBLAH/index' <- Breaks!
get 'cars' to: 'cars#index' <- works
I assume it's some kind of rails magic / sugar, but I can't find anything about this type of situation.
Thanks!
When you define:
get 'carsBLAH/index'
By default Rails looks for index action within CarsBLAHsController. It would break if:
CarsBLAHsController is not defined.
CarsBLAHsController exists but index method is not defined.
You could specify the controller and action to execute for a route with:
# executes CarsController#index
get 'carsBLAH/index', to: 'cars#index'
Suggest reading Rails Routing from the Outside In for details.

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.

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

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

How to Use Rails 3 Routes with Dynamic Segments

In my Rails 3.2 application, there is an Exercise model with attributes muscle_group and grade_level. I've defined the following route with dynamic segments for it in config/routes.rb:
# config/routes.rb
match "/:muscle_group/grade-:grade_level/:id" => "exercises#show"
Running bundle exec rake routes confirms that the route does indeed exist:
/:muscle_group/grade-:grade_level/:id(.:format) exercises#show
The database contains an Exercise record with:
id = 5
muscle_group = "abdominal"
grade_level = 1
And yet when I point my browser to http://localhost:3000/abdominal/grade-1/5, I get:
Routing Error
No route matches [GET] "/abdominal/grade-1/5"
Try running rake routes for more information on available routes.
How can I get this route with dynamic segments to work?
The route declaration I was using was almost functional. For some reason there is an issue with using a hyphen and not having grade_level as its own /-separated substring. When I switched to using the route declaration:
match "/:muscle_group/grade/:grade_level/:id" => "exercises#show"
Instead of the original:
match "/:muscle_group/grade-:grade_level/:id" => "exercises#show"
http://localhost:3000/abdominal/grade/1/5 is then a valid route.
I would prefer to have this route with the hyphen and wish I knew how to make that work, but it is not a top priority. If anyone knows how to make it work with the hyphen, please let me know.
Action pack in Rails uses the to_param to override how the urls get generated by the URL helpers. Take a look at this article, which explains it.
http://www.seoonrails.com/to_param-for-better-looking-urls.html

Newbie Question:Have to type localhost/controller/index in browser to get index method to display in Rails. Why?

I am new to Rails and I am reading Sitepoint's Simply Rails 2 but I am using Rails 3.0.0. In the book we had just set up our first controller (StoriesController) and fired up the server and typed in http://localhost:3000/stories and it should have displayed the index.html.erb file but instead when I type in that url I get "Routing Error: No route matches "/stories" but when I type in http://localhost:3000/stories/index it works properly. Can somebody explain to me why rails is not loading the index.html.erb file implicitly when I go to localhost/stories?
Depending on how you created your routes (in config\routes.rb). Unfortunately, if you scaffold a controller, rails now generates a route like this:
get 'posts#index'
If it is a restful-controller, you better write
resources :posts
Or if it is a special controller (with only an index action) you could write
match '/posts' => 'posts#index'
To provide the fallback match ':controller(/action(/:id(.:format))) is generally avoided. Because it opens up all your controller-methods. The preferred way is that you declare explicitly how to access your site.

Resources