Rails 3 Engine routes resolve 'match' to the assets path - ruby-on-rails

I'm so confused. Here is my situation. Recently I split the application I'm currently working on to three Rails Engines. One of the engines is carrying about user management. I'm using devise for that. By default device is using a route called (user_route) to redirect users after they log in, so I defined it in the routes.rb file of the engine.
So, the long story short:
In the routes.rb of the main application I have:
mount BackOffice::Engine, at: '/bo'
Than in the routes.rb of the BO Engine I have:
match 'user/logged_in' => 'users#logged_in', as: 'user_root'
The whole routes.rb in the engine is:
BackOffice::Engine.routes.draw do
devise_for :admins, {
class_name: 'BackOffice::Admin',
module: :devise,
}
devise_for :users, {
class_name: 'BackOffice::User',
module: :devise,
}
resources :admins
resources :users
resources :life_promotions
match '' => 'life_quotations#index', as: 'life_quotations'
match 'user/logged_in' => 'users#logged_in', as: 'user_root'
root to: 'life_quotations#index'
end
And than if I go do like this:
module BackOffice
class ApplicationController < ActionController::Base
before_filter lambda { raise user_root_path }
end
end
I see the following result:
/assets?action=logged_in&controller=back_office%2Fusers
Which is far from 'user/logged_in'. And also it stops me from using the default Devise behavior which is kind of convenient for me. But the most important is that I really can't figure out what is going on.

Rails.application.routes.draw do
Needs to be used intead of
BackOffice::Engine.routes.draw do
in your engine, cause the match you have in your engine routes only exists in the namespace of your engine, rather than the whole application.

Related

Devise scoped member routes issue

I'm using devise for sign up and ActiveStorage for image upload. For the delete/purge function to work I have this route
devise_scope :user do
scope module: :users do
resources :registrations do
member do
delete :delete_image_attachment
end
end
end
end
But another place in my routes file I have this route
devise_for :users, controllers: {:registrations => "users/registrations"
}
It makes some of my pages not working. I have read somewhere that it's because registrations are declared two times. How can I make it work?
Any help would be much appreciated
If you use resources :registrations, only: [] do... that will create the parent route that you need without overwriting any of the routes provided by devise. Allowing you to make your nested routes :D

Resource in and outside namespace

I have the following routes.rb:
resources :users
namespace :vmt do
resources :dashboards do
resources :users
resources :evaluation_units
resources :orga_units
end
end
I want to set the user in an overall context and nested in a single dashboard context within a namespace. The users-Controller is not in the namespace. So when I open the path /vmt/dashboards/1/users in browser, I get the following Routing Error
uninitialized constant Vmt::UsersController
So how can I specify, that in this resource
namespace :vmt do
resources :dashboards do
resources :users
that the controller is not in a namespace? I tried to set the controller explecitly with
resources :users, controller: 'user'
but it's still in the vmt namespace.
Using scopes will point rails to the proper url, but does not seem to provide the same useful route url helpers. We can, however, use / to point to the 'top level' controller.
Say you have two routes we want to display the users on:
/users and /admin/users
resources: users
namespace :admin do
resources :users, controller: '/users' # 'users' alone would look for a '/admin/users_controller'
end
With this, we can continue to use the url helper admin_users_path
(Note: Not a rails expert, there may be a way to create url helpers for scopes, or some other solution. Above tested on rails 5.2)
My original answer didn't work in the end, once you're inside a namespaced scope within a route you can't get out anymore.
The easiest way to re-use your logic is to create a Vmt::UsersController like so:
class Vmt::UsersController < ::UsersController
end
You can specify a different module with the module key.
For example:
resources :users, module: nil
Edit: I'm not 100% sure if this will work inside a namespace. If not, you can change it to a scope, and add the module explicitly to the other resources.

Is there a way to change the Routes URL without Impacting the controller its is pointing and paths used in the VIEWS

Routes.rb
scope :module => :abc do
namespace :old_namespace do
resources :posts
end
end
How Can I change the old_namespace to new_namespace, So that in my URLS I should see the new_namespace. I have too many views where I have used the previous routes with *_path and *_url methods. I dont want to change them for now. Is there any Rails Way to do this.
Things I have Tried,
scope :module => :abc do
namespace :new_namespace,:as => :old_namespace do
resources :posts
end
end
This Gives me the change in the URLS I need but Also, Gives me and Error
uninitialized constant Abc:NewNamespace
This is expecting me to have constant Abc:NewNamespace, ALthough I want this to use the Old Constant, Abc:OldNamespace, Something Similiar to :controller option in the resources for the namespace
You Simply do this:
scope module: 'abc/OldNamespace' do
resources :posts, path: 'new_namespace/posts'
end
here you are saying,
use abc::OldNamespace
use new_namespace/posts as URL path for posts resource.
This should work too, let me if this doesn't
I used this,
namespace :new_namespace,:as => :old_namespace, :module => :old_namespace do
This is working now.

Rails 3.2 routing error with scope

This problem has taken all day of mine...
Well;
I'm just trying to put all of my administration pages inside an /admin directory and to receive them via domain/admin style only. I've tried to make it run with this guide.
According to that official guide, what I'm looking for is using scope in my routes.rb file. BECAUSE, I have used named routes tones of times inside my pages. I do not want my program_path named route to change admin_program_path since I have 28 different usage of it.
So I'm supposed to use scope instead of namespace.
Issue is: I can not make scope work with my project.
Here is my routes.rb
scope "/admin" do
get "access/login"
get "access/index"
match "access/login_attempt", to: "access#login_attempt"
match "access/logout", to: "access#logout"
resources :admin_users
root to: 'programs#index'
resources :programs
resources :program_categories
resources :program_subcategories
resources :articles
resources :pictures
match '/kategoriler/:id' => 'program_categories#show'
match '/kategoriler' => 'program_categories#index'
match '/kategori/yeni' => 'program_categories#new'
match 'program/yeni' => 'programs#new'
match 'programlar' => 'programs#index'
match '/progam_categories/select_category/:program_id' => 'program_categories#select_category'
match '/program_subcategories/select_subcategory' => 'program_subcategories#select_subcategory'
match '/program_subcategory/add_subcategory' => 'program_subcategories#add_subcategory'
end
Here is my controller beginning :
class ProgramsController < ApplicationController
Just like told here:
If you want to route /admin/posts to PostsController (without the Admin:: module prefix), you could use
scope "/admin" do
resources :posts, :comments
end
As a result, what am I getting?
This error message:
Routing Error
uninitialized constant ProgramsController
Whichever controller I try to access, error changes that way.. Such like uninitialized constant ProgramCategoriesController , uninitialized constant ProgramSubcategoriesController etc...
I've tried to place application_controller both inside admin folder and root of controllers directory... No way.
Where is my mistake here? :(
Thanks in advance...
Try with :module parameter:
scope '/admin', :module => 'admin' do
# ...
end
The assumption is that your controllers are in Admin module namespace, so they start with 'Admin::'.
[EDIT]
It is response to your problem in comments below about path conflicts. You can use :as parameter, for example:
scope '/admin', :module => 'admin', :as => 'admin' do
# ...
end
You can check it with rake routes. All routes in the admin scope should now begin with 'admin_'

How do you render a rails partial that is outside of a namespace in a view that is inside of a namespace?

I've got a 'static' controller and static views that are pages that don't utilize ruby in their views. For these pages, I have a sitemap partial that is generated programatically and used in the application layout file. Namespaced routes still use the application layout file but are taking the static routes and trying to namespace them too.
Here's the relevant portion of the route file:
namespace :admin do
resources :verse_categories
resources :verses
resources :songs
resources :flowers
resources :visits, :except => [:new, :create]
end
match ':action' => 'static'
root :to => 'static#home'
Here's the error I'm getting:
No route matches {:controller=>"admin/static", :action=>"about"}
Note that about is one of the static pages that the sitemap partial uses.
So, how can I resolve this routing issue so that it's not trying to find my static sites inside of the admin namespace? Any help would be appreciated!
What about:
namespace :admin do
...
get "/about" => "static#about"
end
Or
scope "/admin" do
get "/about" => "static#about"
end
This gist explains how to add directories to the search path for namespaced directories. I ended up doing the following:
class Static::BaseController < ApplicationController
def self._prefixes
super | ["other_directory"]
end
end

Resources