Rails 3 scaffold generator places model classes inside namespace. Example:
rails generate scaffold admin/portfolio
But I want only controllers and views to be placed inside admin namespace.
How can I avoid that?
Rails 3 scaffold generator places model classes inside namespace. Example:
rails generate scaffold admin/portfolio
But I want only controllers and views to be placed inside admin namespace.
How can I avoid that?
Rails 4 generators are a bit different. If you use the scaffold_controller generator it will pre-build all the view files, but by default it will look for a model called Admin::Portfolio. To load the correct model just pass the model name as an argument to the generator.
$ rails g model Portfolio
invoke active_record
create db/migrate/20150822164921_create_portfolios.rb
create app/models/portfolio.rb
invoke test_unit
create test/models/portfolio_test.rb
create test/fixtures/portfolios.yml
$ rails g scaffold_controller Admin::Portfolio --model-name=Portfolio
create app/controllers/admin/portfolios_controller.rb
invoke haml
create app/views/admin/portfolios
create app/views/admin/portfolios/index.html.haml
create app/views/admin/portfolios/edit.html.haml
create app/views/admin/portfolios/show.html.haml
create app/views/admin/portfolios/new.html.haml
create app/views/admin/portfolios/_form.html.haml
invoke test_unit
create test/controllers/admin/portfolios_controller_test.rb
invoke helper
create app/helpers/admin/portfolios_helper.rb
invoke test_unit
invoke jbuilder
create app/views/admin/portfolios
create app/views/admin/portfolios/index.json.jbuilder
create app/views/admin/portfolios/show.json.jbuilder
This will give you a namespaced controller and views that reference the non-namespaced model.
rails generate model Portfolio
rails generate controller Admin::Portfolios
@RubyDev was right to suggest Ryan Bate's Nifty Generators, but I don't know why he said to use the --skip-model option.
Nifty Generators will actually do exactly what you are asking for. Simply add it to your Gemfile:
gem "nifty-generators"
and run:
rails g nifty:scaffold Admin::Portfolio name:string
This will create everything a normal scaffold would with the controllers and views in an 'admin' namespace, but the model not in namespace.
Updated as per @tybro0103
Use nifty:generators: https://github.com/ryanb/nifty-generators
rails generate nifty:scaffold Admin::Portfolio
If you have already generated the model or scaffold without namespace and would like to do it again for admin namespace, you can skip model:
rails generate nifty:scaffold Admin::Portfolio --skip-model
If you would like the scaffold to generate views with all fields, please put the field names again, e.g:
rails generate nifty:scaffold portfolio name:string
rails generate nifty:scaffold Admin::portfolio name:string --skip-model
I usually do the two together so its easy to just go to previous command and edit it to add Admin:: & --skip-model.
You can fairly simply create your own generators and do whatever you want with them:
In Rails 4:
#config/application.rb
config.generators do |g|
g.scaffold_controller :my_controller
end
and
#lib/generators/rails/my_controller/my_controller_generator.rb
class Rails::MyControllerGenerator < Rails::Generators::ScaffoldControllerGenerator
def class_name
([file_name]).map!{ |m| m.camelize }.join('::')
end
def table_name
@table_name ||= begin
base = pluralize_table_names? ? plural_name : singular_name
([base]).join('_')
end
end
end
Will remove the model namespacing.
Bear in mind if you are generating a scaffold_controller on its own you'll need to explicitly call your custom generator: rails g my_controller 'account/users'
Unfortunately this only handles the controller. I'm still searching for a view solution.
You can do this now on Rails (or at least on 5.1) using the following command:
rails g scaffold_controller admin/portfolio --model-name=Portfolio
By specifying --model-name Rails does not automatically tries to guess the model name.
Nifty generators was the right thing some time ago, but now as Rails 4 released it became outdated. If you want to generate admin scaffolding with non-namespaced model you can use rails-admin-scaffold gem.
The best solution
rails generate scaffold admin/theme name:string active:integer position:integer --migration=false
rails generate migration CreateTheme name:string active:integer position:integer
# add t.timestamps in migration
So it does not generate a prefix in the tables