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 }
else
format.html { render action: 'new' }
format.json { render json: @admin_user.errors, status:
:unprocessable_entity }
end
end
end
end
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.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/groups/opt_out.