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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With