Rails 3.2 routing error with scope - ruby-on-rails

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_'

Related

Can't deploy app on heroku, always get "The page you were looking for doesn't exist." error

Hi I'm trying to deploy an app made with help of "Agile web development by S.Ruby" and I always get the same error - The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.
I've already tried to migrate my DB on Heroku - that wasn't the case.I think something is wrong with routes.rb file, but I can't understand what is incorrect exactly,please help me to solve this problem
Here is my routes.rb file:
Depot::Application.routes.draw do
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
get "sessions/create"
get "sessions/destroy"
resources :users
resources :products do
get :who_bought, on: :member
end
scope '(:locale)' do
resources :orders
resources :line_items
resources :carts
root 'store#index', as: 'store', via: :all
end
end
As Michal correctly points out you miss the root path. You have defined a route inside the scope you use to get to the different locales, but not a global root. This is not a Heroku problem, it won't work on your local server either.
So, http://your_server.com/en will work, but http://your_server.com will not.
You need to add a root path outside all scopes, like so:
root 'store#index'
You will have to set a default locale or something like that. You can leave the other root directive inside the scope, as you have named it explicitly (with as: 'store') there won't be any conflict.

routing is too verbose - rails

Suppose I have a model User and I want to add some dashboard namespace. So I create dashbord directory and put inside private_users_controller.rb. Now for routing I put
namespace "dashboard" do
resources :users do
member do
get "show" => "private_users#show"
end
end
end
the problem is that I only want to route the get request having this route /dashboard/users/:id/show. But rake routes shows a bunch of post, delete... routes.
How can I cut those ?
seems like you don't need any of the method from resources definition, so just add a match will be ok.
namespace "dashboard" do
match 'users/:id/show', :to => 'private_users#show'
end
if you insist using resource, then the following will work
scope '/dashboard' do
resources :users, :only => :show, :module => 'private'
end
the 'rake routes' output is like this
GET /dashboard/users/:id(.:format) private/users#show
the trailing 'show' inside the url is not needed.
namespace "dashboard" do
get "users/:id/show" => "private_users#show"
end

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 Engine routes resolve 'match' to the assets path

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.

Not able to route using after_inactive_sign_up_path_for

I overridden RegistrationController my_devise/Registration controller
i overridden the following methos:
def after_inactive_sign_up_path_for(resource)
awaiting_confirmation_path
end
I also have a new method in my RegistrationController named:
def awaiting_confirmation(resource)
do tuff....
end
My routing file looks likethis:
devise_for :accounts, :controllers => {
:registrations => "my_devise/registrations"}
resources :registration do
match "awaiting_confirmation" => "registrations#awaiting_confirmation"
end
I get an error message:
No route matches {:action=>"awaiting_confirmation", :controller=>"registrations"}
What am i doing wrong?
resources :registration do
match "awaiting_confirmation" => "registrations#awaiting_confirmation"
end
Where are you specifying that your registrations controller is in my_devise folder??
You need to specify that manually, because Rails follows conventions, and therefore its looking in the app/controllers directory to find the registrations controller, that you have written yourself.
To get more idea about this, have a look at the output of rake routes command and find the route that rails has generated for it.

Resources