Active admin won't accept my route - ruby-on-rails

I've some problems with getting the routes up n' running for my application.
1.
I want to be able to point active admin to http://admin.lvh.me:3000/
I tried using this code, but it only displays the index page.
# config/routes.rb
scope :admin, constraints: { subdomain: "admin" } do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config.merge(path: "/")
end
# config/initializers/active_admin.rb
config.default_namespace = :admin
The only url that works is http://admin.lvh.me:3000/admin
Is it possible to avoid /admin?
2.
Each exam in my application has many parts.
I want to add a parts button to each exam using this code.
# app/admin/exams.rb
ActiveAdmin.register Exam do
# ...
index do
column :actions do |exam|
link_to "Part", admin_exam_parts_path(exam)
end
default_actions
end
# ...
end
The problem is that admin_exam_parts_path doesn't exist.
# config/routes.rb
scope :admin, constraints: { subdomain: "admin" } do
resources :exams do
resources :parts
end
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config.merge(path: "/")
end
rake routes | grep /admin/exams/:exam_id/parts doesn't return anything.
What I'm I doing wrong?
I'm running
active admin 0.5.1
rails 3.2.12
ruby 1.9.3

For your first problem i have this on my routes.rb
root to: "admin/dashboard#index"
After resources and before devise_for, and works very fine!.
Good luck!

"admin" is the default namespace for all resources.
you can turn off a resources namespace explicitly in the registration block like this:
ActiveAdmin.register Exam, :namespace => false do
...
end
as far as you has_many: :parts association, can you post the code for your registration block for Parts? There are several ways I might set up this relationship depending on what I was trying to accomplish. So the more context you can give me, the better

Related

How to have one resource in routes for namespace and root path altogether - Rails 4

I am making a custom admin panel in a namespace "admin".
I have resources "courses" within that namespace.
But I would also like a route to "courses" that is not in that namespace:
eg: BOTH localhost:3000/admin/courses AND localhost:3000/courses
It's OK if this requires different controllers.
My concern is that its not really DRY if i have both resources for the same route.
namespace admin do
resources :courses
end
and just
resources :courses
Is there a way to have one resource be shared between namespace and without namespace, or is the example above the way to go?
Oh wait ! There's also the possibility to use concerns !
concern :shared_actions do
resources :courses
resources :something_else
end
namespace :admin do
concerns :shared_actions
end
concerns :shared_actions # Will add it to the root namespace ^^
EDIT : apparently this is what this guy also tried to do :D
I'm not really sure I understand what you mean, but
namespace :something is actually a shorthand for scope :something, module: :something, as: :something
scope :something will add /something/ as a URL prefix
scope module: :something will add /something as a controller prefix (controllers will be fetched under controlelrs/something/the_controller.rb
scope as: :something will add the something as a prefix for path helpers
Now it's totally fine to have both in your routes
resources :courses
# Will generate "/courses/", "/courses/new", "/courses/1/edit", ...
# And will point to `controllers/courses_controller.rb`
namespace :admin do
resources :courses
end
# Will generate "/admin/courses/", "/admin/courses/new", "/admin/courses/1/edit", ...
# And will point to `controllers/admin/courses_controller.rb`
Does this answer your question ?

Rails routes adding resource_id to url

Hi i have a rails app and i use different namespaces in it like user, admin etc.
My url's under user namespaces as
'/user/:controller_name' but i want to
use it like
'/library/:library_id/user/:controller_name'
Users has one-to-one relationships with libraries. And i can get current users library id.
when i tried to make it with path parameter in route like
namespace :user, path: "library/user" do
...
end
it is working but i couldnt get id.
Is it possible to do this?
This does not work?
namespace :user, path: "library/:library_id/user" do
...
end
I guess, this is wat you are looking for
resources :libraries do
namespace :users do
resources :resource_name
end
end
# Output for me
/libraries/:library_id/users/controller_name/new(.:format)
/libraries/:library_id/users/controller_name/:id/edit(.:format)
.
.
This should work for you:
resources :libraries do
member do
resources :user do
collection do
resources :controller_name
end
end
end
end

Subdomain based resources in Rails

I am looking for a simple way to replace all my routes matching mydomain.com/resources/xx by xx.mydomain.com with Rails 4.
Would anyone have an idea to do that easily and that would work with nested resources as well?
Thanks, Joris
Constraints
What you're looking for is constraints in your routes, specifically that you're looking to use one to determine whether you have a subdomain that you can access
There are a number of resources on how to achieve this:
Basecamp-style subdomains by DHH
Subdomains Railscast
The bottom line is that you'll probably have to create a custom subdomai constraint, which you can then use the standard routing structure for:
#lib/subdomain.rb
class Subdomain
def self.matches?(request)
if request.subdomain.present? && request.subdomain != 'www'
account = Account.find_by username: request.subdomain
return true if account # -> if account is not found, return false (IE no route)
end
end
end
#config/routes.rb
constraints(Subdomain) do
get "/", to: "controller#action"
resources :posts #-> subdomain.domain.com/posts
...
end
The above is untested - I also found the following with Rails' documentation:
#lib/subdomain.rb
class Subdomain
def initialize
#accounts = Account.all
end
def matches?(request)
if request.subdomain.present? && request.subdomain != 'www'
#accounts.include?(request.subdomain)
end
end
end
#config/routes.rb
constraints: Subdomain.new do
get "/", to: "controller#action"
resources :posts #-> subdomain.domain.com/posts
...
end
Here how I have done it before in a Rails 3 app:
constraints :subdomain => /ambassador/ do
namespace(:influencer, :path => '/') do
root :to => 'home#index'
match 'home' => 'sweepstakes#index', :as => :influencer_home
resources :sweepstakes
resources :associates
resources :widgets
resources :sessions
resources :reports do
resource :member
end
match 'faq' => 'info#faq'
end
end
Be sure to put this block towards the top of the routes.rb file so it takes precedence.
You can of course nest your resources in here like normal.

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.

Route a controller to namespace :admin to /admin

I feel like this may be a dumb question, but it's late and my head is melting a bit.. So I appreciate the assistance.
I'm trying to map the url http://localhost:3000/admin to a dashboard controller but i'm epically failing. Maybe this isn't even possible or the completely wrong idea but anyway my routes looks like this and yes
namespace :admin do
resources :dashboard, { :only => [:index], :path => '' }
...
end
and my simple dashboard_controller.rb
class Admin::DashboardController < ApplicationController
before_filter :authenticate_user!
filter_access_to :all
def index
#schools = School.all
end
end
and my view is located in views/admin/dashboard/index.html.erb
thanks for any input
If all you're trying to do is route /admin to that dashboard controller, then you're overcomplicating it by namespacing it like that.
Namespacing with a nested resource like that would mean that it would be /admin/dashboards for the :index action instead of having a clean /admin route (and you can verify that by running rake routes at the command line to get a list of your routes).
Option 1: You meant to namespace it like that
# putting this matched route above the namespace will cause Rails to
# match it first since routes higher up in the routes.rb file are matched first
match :admin, :to => 'admin/dashboards#index'
namespace :admin do
# put the rest of your namespaced resources here
...
end
Option 2: You didn't mean to namespace it like that
Route:
match :admin, :to => 'dashboards#index'
Controller:
# Remove the namespace from the controller
class DashboardController < ApplicationController
...
end
Views should be moved back to:
views/dashboards/index.html.erb
More info: http://guides.rubyonrails.org/routing.html
Regarding to http://guides.rubyonrails.org/routing.html I prefer this
namespace :admin do
root to: "admin/dashboards#index"
resources :dashboard
end
Try this:
namespace :admin do
root to: 'users#index' # whatever. Just don't start with /admin
#resources :dashboards <= REMOVE THIS LINE !
end

Resources