Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Rails Way - Namespaces

I have a question about how to do something "The Rails Way". With an application that has a public facing side and an admin interface what is the general consensus in the Rails community on how to do it?

Namespaces, subdomains or forego them altogether?

like image 550
Carlos Avatar asked Sep 23 '08 04:09

Carlos


People also ask

What are namespaces in Rails?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.

How do you use namespaces in Ruby?

The namespace in Ruby is defined by prefixing the keyword module in front of the namespace name. The name of namespaces and classes always start from a capital letter. You can access the sub members of double with the help of :: operator.

How do I see routes in Rails?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

What are restful routes in Rails?

The Rails router is responsible for redirecting incoming requests to controller actions. The routing module provides URL rewriting in native Ruby. It recognizes URLs and dispatches them as defined in config/routes.


2 Answers

There's no real "Rails way" for admin interfaces, actually - you can find every possible solution in a number of applications. DHH has implied that he prefers namespaces (with HTTP Basic authentication), but that has remained a simple implication and not one of the official Rails Opinions.

That said, I've found good success with that approach lately (namespacing + HTTP Basic). It looks like this:

routes.rb:

map.namespace :admin do |admin|
  admin.resources :users
  admin.resources :posts
end

admin/users_controller.rb:

class Admin::UsersController < ApplicationController
  before_filter :admin_required
  # ...
end

application.rb

class ApplicationController < ActionController::Base
  # ...

  protected
  def admin_required
    authenticate_or_request_with_http_basic do |user_name, password|
      user_name == 'admin' && password == 's3cr3t'
    end if RAILS_ENV == 'production' || params[:admin_http]
  end
end

The conditional on authenticate_or_request_with_http_basic triggers the HTTP Basic auth in production mode or when you append ?admin_http=true to any URL, so you can test it in your functional tests and by manually updating the URL as you browse your development site.

like image 115
Ben Scofield Avatar answered Sep 24 '22 20:09

Ben Scofield


In some smaller applications I don't think you need to separate the admin interface. Just use the regular interface and add admin functionality for logged in users.

In bigger projects, I would go with a namespace. Using a subdomain doesn't feel right to me for some reason.

like image 5
psst Avatar answered Sep 23 '22 20:09

psst