Let scaffold controller generators generate namespaced controllers for non-namespaced models

A common scenario is wanted to generate scaffold controllers within an admin namespace.

When attempting to do this with the standard scaffold_controller generator, it assumes you are doing it for a namespaced model as well. Therefore, out of the box many of the generated code doesn’t work.

Lets say you run rails g scaffold_controller admin/users, for an existing model User that is not within a namespace.

The resulting controller has the namespace correctly, but the model is namespaced too (not the desirable result):

class Admin::UsersController < ApplicationController
    # code omited for demo purposes
    def create
      @admin_user = Admin::User.new(admin_user_params)

      respond_to do |format|
        if @admin_user.save
          format.html { redirect_to @admin_user, notice: 'User was successfully created.' }
          format.json { render action: 'show', status: :created, location: @admin_user }
          format.html { render action: 'new' }
          format.json { render json: @admin_user.errors, status: :unprocessable_entity }

There are multiple problems here - there is no model such as Admin::User, and furthermore redirect_to @admin_user would never work - the correct thing to do here would be:
redirect to admin_user_path(@admin_user)

We should be able to generate a namespaced controller with a different namespace for the model.

IMO the scaffold controller is not designed to be used for building all parts of your application. It’s soul purpose is for you to put up a quick and dirty controller.

Along similar lines: I strongly doubt your admin controllers are going to need to respond to json.

I would strongly recommend AGAINST ever using scaffold for any code within rails and stick entirely with writing controllers by hand so you get all the stuff you want with none of the crap you don’t.

It’s actually this same viewpoint which has lead to the removal of the scaffold controller from the getting started guide, too.

I completely agree with Ryan that you should not completely rely on the generated scaffold code. However it is a good starting point when you need a basic CRUD controller. It saves some typing and you can easily get rid of stuff you don’t need. As of the requested functionality. I remember a PR adding an option along these lines. Probably you can already achieve your goal by using --model-name. See https://github.com/rails/rails/pull/11432 for more details. Note that this feature is only on master and will be released with 4.1.0. You can already get to it by using 4.1.0.beta1.


– Yves

Yes I know it’s not for all parts of your application, but often in admin interfaces you need something quick and dirty that does CRUD - the scaffold controller is a good starting point for that. Yes you have to go back and delete some code, but it’s way faster than building it all from scratch.

And you would want it to respond to json if you were using something like angular on the front end to do two way databinding.

Yves - that’s exactly the feature I’m looking for. Thanks for pointing it ou!