Note that there are some explanatory texts on larger screens.

plurals
  1. PORails : Benefit of URL path modification vs Namespace?
    primarykey
    data
    text
    <p><em>Many people talk about namespace as a way to change the URL that lead to their controller with a prefix (i.e. : /admin/movies instead of /movies)</em></p> <h2>Changing the URL path</h2> <p>The <a href="http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing" rel="nofollow">official doc</a> explains that if we want to change the URL that lead to our controller with a prefix, we just have to change our resource in config/route.rb.</p> <p>From this :</p> <pre><code> resources :movies </code></pre> <p>to this :</p> <pre><code> resources :movies, :path =&gt; "/admin/movies" </code></pre> <h2>Implementing namespace</h2> <p>After a lot of googsearches, I'm wondering why so many people like to have namespace and <strong>what are the adavantages of namespace versus just modifying the URL path leading to a specific controller</strong> in the router file : myapp/config/route.rb</p> <p>To implement a namespace, the official doc explains that you need rather the following modifications. <strong>You will see that this is a lot of work</strong> :</p> <pre><code> namespace :admin do resources :movies end </code></pre> <p>...and moves your movies_controller.rb controller to app/controllers/admin directory.</p> <p>But if you follow these instructions, you'll get this error :</p> <p>*"Expected /var/www/myapp/app/controllers/admin/movies_controller.rb to define Admin::MoviesController"*</p> <p>*Then you realized that Rails expect this 'alien' writing at the begining of your movies_controller.rb : "Admin::"*</p> <p>So you change the first line of your movies_controller.rb with :</p> <pre><code> class admin::MoviesController &lt; ApplicationController </code></pre> <p>instead of :</p> <pre><code> class MoviesController &lt; ApplicationController </code></pre> <p>But you again get another error : <em>"undefined local variable or method `admin' for main:Object"</em></p> <p>Then you realized you forgot Ruby classes MUST be declared with a starting uppercase.</p> <p>So you add a starting uppercase to your Ruby class :</p> <pre><code> class Admin::MoviesController &lt; ApplicationController </code></pre> <p>But you still get an error :</p> <p>*Missing template admin/movies/index, application/index with {:locale=>[:"fr-CH"], :formats=>[:html], :handlers=>[:erb, :builder, :rxls, :coffee, :haml]}. Searched in: * "/var/www/myapp/app/views"*</p> <p>What the hell...? Oh did the doc forgot to say as well that controller's corresponding views must as well be moved to an admin directory within the app/view/ ?</p> <p>So you create a directory and move your views to app/view/admin/ And you still get the same error.</p> <p>Then you realized you forgot to include the movies folder itself within app/view/admin So you did it.</p> <p>You still got an error message : *undefined method `movies_path' for #&lt;#:0xa817c0c>*</p> <p><strong>This time you know that routing, controller and view work</strong> but you still have to change the pathes on all your views...</p> <p>Moreover, if you are using the "<strong>respond_with</strong>" method in your controller file, you have to include the namespace as parameter. This is an example for the index operation :</p> <pre><code> def index @movies = Movie.all respond_with(:admin, @movies) end </code></pre> <p>Moreover if you are using <strong>declarative_authentification</strong> gem (similar as cancan gem), you have to prefix the argument of your permited_to? method with the namespace. For example (with HAML syntax) :</p> <pre><code>- 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) </code></pre> <p><strong>You realize you were going to create a namespace just for the url path conveniance, so you decide to give up, roll back your modif, and just add the following line in your route.rb file :</strong></p> <pre><code> resources :movies, :path =&gt; "/admin/movies" </code></pre> <p>Which works immediately like a charm.</p> <p><strong>As testing purpose</strong> I created a new fake project "testapp" including <strong>namespace with the generator</strong>. I performed a "rails generate scaffold admin/movie title:string" to check how does the generator handle namespace. The resulting app/controller/admin/movies_controller.rb is rather different from what we expected it to be. There are additionnal prefix "Admin::" in front of each instance variable declaration. Here for instance the index method :</p> <pre><code> def index @admin_movies = Admin::Movie.all respond_to do |format| format.html # index.html.erb format.json { render json: @admin_movies } end end </code></pre> <p>I guess that this is because the generator as well moved the model movie.rb into app/models/admin directory</p> <p>Moreover I see that there is another model in app/models : admin.rb Which contains :</p> <pre><code> module Admin def self.table_name_prefix 'admin_' end end </code></pre> <p><strong>I have no idea how this method will be used nor by who ?</strong></p> <p>I notice as well that my table will be named "admin_movies" in the /db/migrate/2012blabla_create_admin_movies.rb</p> <pre><code> class CreateAdminMovies &lt; ActiveRecord::Migration def change create_table :admin_movies do |t| t.timestamps end end end </code></pre> <p>Well, it seems there will have a lot more work (and mess-up) than I expected in order to put in place a namespace...</p> <p>For what benefit/advantage ???</p> <ol> <li>To use another url path ? This small modification in my controller would be far more easier : resources :movies, :path => "/admin/movies"</li> <li>To make it possible to use two different controllers using a same name ? Wouldn't it be easier to just put a prefix to my controler name than do all this mess-up ?</li> </ol> <p>Of course some of the above errors could have been avoided by being more vigilant. But you know, we are just human, and when it's late in the evening while coding, maybe you wished this part of rails would have been, let's say, more human adapted...</p> <p>I really don't understand why people get so excited to have namespace. I woudld be gratefull, if an expert could explain what am I missing.</p> <p>Many Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload