Prefixed route path gives error in Rails 4 - ruby-on-rails

After upgrading to Rails 4 a route with a prefixed name and slash is throwing an error.
actionpack-4.0.1.rc1/lib/action_dispatch/routing/mapper.rb:239:in `default_controller_and_action':
'MyEngine/dashboard'
is not a supported controller name. This can lead to potential routing problems.
In routes.rb I have
Rails.application.routes.draw do
mount MyEngine::Engine => "/foo", :as => 'my_engine'
match 'dashboard' => 'MyEngine/dashboard', via: :get
And in the mounted engine MyEngine:
MyEngine::Engine.routes.draw do
match 'dashboard' => 'dashboard#index', via: :get
This works well in Rails 3.2, but in Rails 4 the slash in 'MyEngine/dashboard' throws the error.

Using an engine, you can directly create routes to the engine's controllers and actions in your routes file like this:
Rails.application.routes.draw do
mount MyEngine::Engine => "/foo", :as => 'my_engine'
get 'dashboard' => 'dashboard#index'
end

I think it's not possible to set a route in the host application to a controller in a mounted engine (mounted on "/foo") at the top level, e.g. /foo/dashboard calls the mounted engine's 'dashboard#index' action, and I want /dashboard to do the same.
I'm adding a controller of the same name in the host application and doing a redirect to the mounted engine controller action.

Simply, change this line
match 'dashboard' => 'dashboard#index', via: :get
as
get 'dashboard' => 'dashboard#index'

Related

What is this gotcha inRails application.routes.recognize_path when using the root path of a mounted engine?

I am developing an engine and I need to programmatically find which controller action will be used for any path of either the host app or my engine. However, there seems to be a gotcha when resolving the root PATH of an engine.
The dummy's routes file is:
Rails.application.routes.draw do
mount Authz::Engine => '/authz', as: :authz
#...
end
The engine's routes file is:
Authz::Engine.routes.draw do
root 'home#index'
resources :foos
#...
end
Somewhere in the main app I am doing the following I don't understand what is the gotcha here...
Rails.application.routes.recognize_path authz.root_path, method: :get
# => No route matches "/authz"
Strangely if I do the following, I get the correct answer.
Rails.application.routes.recognize_path authz.root_url, method: :get
# => {:controller=>"authz/home", :action=>"index"}

Why doesn't my Rails engine mount under the correct path?

I have a Rails engine with these routes:
Project::Engine.routes.draw do
post '/sessions' => 'project/sessions#create'
get '/login' => "project/sessions#new", as: :login
get '/logout' => "project/sessions#destroy", as: :logout
root to: 'project/home#dashboard'
end
In my main Rails app, I have the engine's routes mounted like this:
CMS::Application.routes.draw do
mount Project::Engine => '/project', as: 'project'
end
I would think that this would mean that I would now have a /project/login route and /project/logout /project/sessions routes, but instead they are all under root(/sessions, /login, /logout).
If this isn't going to do what I want, then what exactly is going on here when I specify the path here? I'm using Rails 4.2.1.
I believe after testing that the problem is that you use 'project/[...]' when drawing the routes inside the engine.
You mount the engine as project in the main app. Project is out of scope when referenced inside the engine.
Changing to the following should make it work.
Project::Engine.routes.draw do
post '/sessions' => 'sessions#create'
get '/login' => "sessions#new", as: :login
get '/logout' => "sessions#destroy", as: :logout
root to: 'home#dashboard'
end
Then mounting the engine with:
CMS::Application.routes.draw do
mount Project::Engine => '/project', as: 'project'
end
Gives the paths:
/project
/project/sessions
/project/login
/project/logout
See:
http://guides.rubyonrails.org/engines.html#mounting-the-engine
http://guides.rubyonrails.org/engines.html#routes

`default_controller_and_action': missing :controller (ArgumentError) in Ruby 3

I am creating a website were users can post jobs and users can login,register etc. I have created my jobs model and created my user model for the login/register part. When I try to load rails server I keep getting this error below and cannot figure out what I'm doing wrong or how to fix. I originally was trying to use devise and create the users model but was having issues so I deleted the files for it. I am wondering if I deleted something or if I am missing something in my routes.rb file. Can someone help or point me in the right direction? I will post my routes.rb file as well. Thanks for any guidance as I am still new to rails. The only thing I added to the routes.rb file was root :to => "sessions#login" and below that. Im sure other info in this was added when i created the models and controller.
/home/whitey7/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/mapper.rb:181:in `default_controller_and_action': missing :controller (ArgumentError)**
Routes.rb
Application.routes.draw do
get "sessions/login,"
get "sessions/home,"
get "sessions/profile,"
get "sessions/setting"
get "users/new"
resources :jobs
root :to => "jobs#index"
root :to => 'home/index'
root :to => "sessions#login"
match "signup", :to => "users#new"
match "login", :to => "sessions#login"
match "logout", :to => "sessions#logout"
match "home", :to => "sessions#home"
match "profile", :to => "sessions#profile"
match "setting", :to => "sessions#setting"
I think you still have a route in routes.rb which in invalid. Please recheck all the routes, corresponding controller and action. Please also share the full error trace, that way we can pin-point the issue.
Please check if the jobs controller is still there. Because this is the first root directive in your routes declarations (and still, it does not make sense to have more than one), Rails is checking if this root route is available. It seems that the jobs controller is missing and causing this error.
At first, check Rails routing documentation. I think you are getting this error because you are unable to define route file. The problem I figure out in your route file are :-
a. You have three different root in your route file.
root :to => "jobs#index"
root :to => 'home/index'
root :to => "sessions#login"
b. You are defining same routes multiple times.
get "sessions/login,"
get "sessions/home,"
get "sessions/profile,"
get "sessions/setting"
get "users/new"
match "signup", :to => "users#new"
match "login", :to => "sessions#login"
match "home", :to => "sessions#home"
match "profile", :to => "sessions#profile"
match "setting", :to => "sessions#setting"
The solution might be as follows :-
a. Fix the root path at first. Which path you want to make root whether it is jobs index or home index or sessions login.
b. I think you are trying to define your routes as such
match "signup", :to => "users#new", via: :get
match "login", :to => "sessions#login", via: :get
match "home", :to => "sessions#home", via: :get
match "profile", :to => "sessions#profile", via: :get
match "setting", :to => "sessions#setting", via: :get

RESTful routing in Rails 3 app - some advice / pointers

I have an app, locally in development without a subfolder, and in production i have it deployed under /myappname/
So, locally I have http://myapp.dev and in production http://mydomain.com/myappname
which my root route does:
root :to => 'products#list'
which works great, even in production.
Now, i have a default match action:
match '/:controller(/:action(/:id))'
which breaks in production, so i started trying to build a restful route, but i need some help... I can't wrap my head around the routing. I think i have the proper start (with scope, below)
#PRODUCTION ROUTES
scope '/myappname' do
#WHAT WOULD GO HERE?
end
format would be /myappname/products/show/15
Hm. I would expect all the routes to work relative to the "home page", so why don't you just go with
resources :users
or any other route definition from the examples in config/routes.rb?
#PRODUCTION ROUTES
scope "/mothers" do
#ROOT
root :to => 'rings#list'
match '/rings/:id' => "rings#show", :as => :ring
end
#DEVELOPMENT ROUTES
root :to => 'rings#list'
match '/rings/:id' => "rings#show", :as => :ring

Adding Routes to Rails' Spree E-Commerce

I just recently upgraded to 1.0.3, and the routes.rb file in my config/routes folder seems to ignore all my custom routes.
MY routes.rb
JollyStore::Application.routes.draw do
# Mount Spree's routes
mount Spree::Core::Engine, :at => '/'
root :to => 'pages#index'
namespace :admin do
resources :wysiwygs
end
match 'about_us/', :to => "pages#about_us"
match 'services/', :to => "pages#services"
match 'raw_resources/', :to => "pages#raw_resources"
match 'contact_us/', :to => "pages#contact_us"
match 'privacy_policy/', :to => "pages#privacy_policy"
match 'return_policy/', :to => "pages#return_policy"
match 'refund_policy/', :to => "pages#refund_policy"
match 'cancellation_policy/', :to => "pages#cancellation_policy"
match 'delivery_shipping_policy/', :to => "pages#delivery_shipping_policy"
end
If I run bundle exec rake routes, it returns all the approriate routes. But when I try to reach that specific page, I get :
undefined local variable or method `about_us_path'
Or the same error for every link that is within my custom routes. Somehow my routes are being ignored. Does anyone know a way to circumvent this issue?
I encountered the same error and found this solution, which solved it by prefixing main_app, before each of my_paths/_urls. In my case, these were links used in one of the /override.rb files.
So, try: main_app.about_us_path.
You can add new routes in the Spree using following block in routes.rb file
Spree::Core::Engine.routes.prepend do
# Your new routes
end
For me prepend did not work.
for me draw did the work:
Spree::Core::Engine.routes.draw do
resources :orders, except: [:new, :create, :destroy] do
post :my_order, on: :collection
end
end

Resources