Rails routing example - ruby-on-rails

I am trying to port a working rails 2.4 app to rails 4.2. I cannot get the routing to work.
the URL of x.pdf is broken into id.format and passed as params to a report controller.
in Rails 2.4 I have
map.report ':id.:format', :controller => :reports, :action => :generate
in Rails 4.2 I am trying to use
get ':id.:format' => 'reports#generate'
but when i run the app and browse to http://www.example.com/x.pdf, i get the fatal error
ActionController::RoutingError (No route matches [GET] "/x"):
When I run rake routes I get:
Prefix Verb URI Pattern Controller#Action
GET /:id.:format reports#generate
I am using Ruby 2.3.0, Rails 4.2.5, NGINX 1.9.9, Passenger 5.0.23
Any help would be appreciated!

You should change get ':id.:format' to just get ':id'

Related

"No route matches" after installing HyperLoop

I'm getting an error when I start rails in development
ActionController::UrlGenerationError - No route matches {:action=>"index", :client_id=>"bd881667-ee11-441e-a98f-6f6a6005435a-53d08ea16f47a449a12861c8ebf6e1d3", :controller=>"reception"}
It seems to be coming from a link:
reception_index_url
rake routes shows
reception_index GET /reception(.:format) reception#index
so the route is there but I'm guessing :client_id is the problem. I have added mount HyperMesh::Engine => '/rr' to routes.rb
What am I missing?

Rails 4 not showing routes in 'rake routes' but views are working

Pretty much as the title states, I have the following in my routes file:
root to: 'assets#index'
resources :assets do
member do
get :download
end
end
Yet my output for rake routes and visiting rails/info/routes are both simply:
Prefix Verb URI Pattern Controller#Action
root GET / assets#index
However the routes work fine in my views.
I also tried with bundle exec and I've updated to the latest version of bundle as some other posts suggested. It still works for my Rails 3 apps.
:assets is a reserved path in Rails. So you cannot really use it.

HttpHelpers routing methods in mounted rails engine results in uninitialized constant "controller name"

My environment:
Rails 3.2.8
Ruby 1.9.3p194
Fedora 16 x86_64
This problem seems specific to Rails Engines.
It seems that when using the HttpHelpers in a Rails Engine's routes file, I get "uninitialized constant Controller" when accessing a route via a browser. But, if I use a URL matcher in the Engine's routes file, it routes correctly.
Here's how I created a failing example:
$ rails plugin new my_engine --mountable
$ cd my_engine
$ rails g controller things index
$ rails s -p 3005
The controller generator uses the HttpHelpers#get method by default, so at this point the Rails Engine's config/routes.rb file looks like:
MyEngine::Engine.routes.draw do
get "things/index"
end
And, the test/dummy application's config/routes.rb file looks like:
Rails.application.routes.draw do
mount MyEngine::Engine => "/my_engine"
end
So, I should be able to hit http://locahost:3005/my_engine/things/index and see the Things#index view from the Engine. But, instead in the browser I see:
Routing Error
uninitialized constant ThingsController
If I manually change the Engine's config/routes.rb file to:
MyEngine::Engine.routes.draw do
#get "things/index"
match "things/index" => "things#index"
end
... and hit http://locahost:3005/my_engine/things/index, I see the correct Things#index view.
I noticed that when I use the HttpHelpers#get method in the Engine's config/routes.rb file, and run rake routes from the test/dummy directory, I see:
$ rake routes
my_engine /my_engine MyEngine::Engine
Routes for MyEngine::Engine:
things_index GET /things/index(.:format) things#index
But, if I change the Engine's config/routes.rb file to use the URL matcher method, I see:
$ rake routes
my_engine /my_engine MyEngine::Engine
Routes for MyEngine::Engine:
things_index /things/index(.:format) my_engine/things#index
Notice that when using the URL matcher, the controller and action are correctly namespaced under the engine. While, when using the HttpHelpers#get, the controller and action seem to be non-namespaced.
So, my question: Am I doing something wrong here? Or, is this a bug?
Note: I searched the rails issues and didn't see anything directly related to this. Though I did see several other engine and routing issues.

Rails / Cappuccino App Not Loading on Heroku

I have a rails app with a cappuccino front end that I am trying to deploy onto Heroku.
The app works fine when I run it on localhost using WEBrick, but when I push onto Heroku I get the error message ActionController::RoutingError (No route matches "/"):
Here is the contents of the routing file:
CappcourceWs::Application.routes.draw do
resources :transaction_logs
resources :users
end
Is there a route that I have failed to define?
Doesn't look like a heroku-specific problem. My first guess is you need to add a root route, such as:
CappcourceWs::Application.routes.draw do
root :to => 'users#login'
resources :transaction_logs
resources :users
end
...or whatever the appropriate action/view is in your case.

why doesn't this routing work on heroku but works locally?

I have the following in my routes.rb file for Rails 3:
13 namespace :user do
14 root :to => "users#profile"
15 end
I get this error on heroku:
ActionController::RoutingError (uninitialized constant User::UsersController):
I already restarted the application.
I am doing this because I am using devise and this is what it says on the wiki:
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in
The problem is that Rails is expecting there to be a controller within a module called Users because that's what namespace :user infers. Perhaps you meant to use scope instead of namespace?
scope :path => "user" do
root :to => "users#profile"
end
Note: in this situation if you've only got one route it would not be wise to use scope, but if you've got multiple ones with the /user prefix then it would be fine to. If you only had one, I would do this instead:
get '/user', :to => "users#profile"
Heroku environments run in production mode. When you run locally, you run in development mode, which accounts for at least one difference. Try this instead:
RAILS_ENV=production bundle exec rails s
and see if you notice the same error.

Resources