Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Rails scaffold to place model into namespace

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?

like image 419
Alexey Zakharov Avatar asked Sep 24 '10 12:09

Alexey Zakharov


5 Answers

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.

like image 169
Ryenski Avatar answered Nov 10 '22 21:11

Ryenski


rails generate model Portfolio

rails generate controller Admin::Portfolios

like image 23
Jed Schneider Avatar answered Nov 10 '22 21:11

Jed Schneider


@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.

like image 7
tybro0103 Avatar answered Nov 10 '22 22:11

tybro0103


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.

like image 5
Chandresh Pant Avatar answered Nov 10 '22 20:11

Chandresh Pant


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.

like image 3
haris Avatar answered Nov 10 '22 20:11

haris