Rails: benefits of URL modification vs Namespace?

Many people talk about the namespace as a way to change the URL that leads to their controller with a prefix (for example: / admin / movies instead of / movies)

URL path change

The official white paper explains that if we want to change the URL that leads to our controller with a prefix, we just need to change our resource in config / route.rb.

From this:

  resources :movies

:

  resources :movies, :path => "/admin/movies"

Namespace Injection

After many googsearches, I wonder why so many people like to have a namespace and what are the advantages of a namespace, and not just changing the path of the URL leading to a specific controller in the router file: myapp / config / route.rb

, , . , :

  namespace :admin do
    resources :movies
  end

... movie_controller.rb //admin.

, :

* "/var/www/myapp/app/controllers/admin/movies_controller.rb Admin:: MoviesController" *

* , Rails , "" movie_controller.rb: "Admin::" *

, movie_controller.rb :

  class admin::MoviesController < ApplicationController

:

  class MoviesController < ApplicationController

: "undefined ` admin ' main: Object"

, Ruby .

, Ruby:

  class admin::MoviesController < ApplicationController

:

* admin/movies/index, application/index {: locale = > [: "fr-CH" ],: formats = > [: html],: handlers = > [: erb,: builder,: rxls,: coffee,: haml]}. : * "/var/www/myapp/app/views" *

...? , Doc , /view/?

, app/view/admin/ - .

, /view/admin , .

: * undefined `movies_path ' # < #: 0xa817c0c > *

, , , ...

, " response_with" , . :

  def index
    @movies = Movie.all
    respond_with(:admin, @movies)
  end

, declarative_authentification gem ( cancan gem), permited_to? . ( HAML):

- if permitted_to? :index, :admin_movies
  // And prefix as well your path
  = link_to 'Show all my movie", admin_movies_path

- if permitted_to? :show, :admin_movies
  // And prefix as well your path
  = link_to 'Show selected movie", admin_movie_path(@movie)

, URL-, , route.rb:

  resources :movies, :path => "/admin/movies"

.

testapp, . ", / : ", , . /controller/admin/movies _controller.rb , . "Admin::". , , :

  def index
    @admin_movies = Admin::Movie.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @admin_movies }
    end
  end

, , movie.rb app/models/admin

, , / : admin.rb :

  module Admin
    def self.table_name_prefix
      'admin_'
    end
  end

, ?

, "admin_movies" /db/migrate/ 2012blabla_create_admin_movies.rb

  class CreateAdminMovies < ActiveRecord::Migration
    def change
      create_table :admin_movies do |t|

        t.timestamps
      end
    end
  end

, , ( ), , ...

/???

  • URL-? : : ,: = > "//"
  • ? , ?

, , . , , , , , , , ...

, , . , , .

+5
2

, . , .

, , , :

rake generate controller "Foo/Bars"

, .

+1

All Articles