50

I've obtained a project that have controllers (minimal code only) and models, but the views are missing. Is there a way to generate the views only using scaffold or another tool?

0

6 Answers 6

85
rails g scaffold User --migration=false --skip

The --skip means to skip files that already exist. (The opposite is --force.)

If you don't want helpers, --helpers=false.

Sample output after deleting my User views:

      invoke  active_record
   identical    app/models/user.rb
      invoke    test_unit
   identical      test/unit/user_test.rb
        skip      test/fixtures/users.yml
       route  resources :users
      invoke  scaffold_controller
   identical    app/controllers/users_controller.rb
      invoke    erb
       exist      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
   identical      test/functional/users_controller_test.rb
      invoke    helper
   identical      app/helpers/users_helper.rb
      invoke      test_unit
   identical        test/unit/helpers/users_helper_test.rb
      invoke  assets
      invoke    coffee
   identical      app/assets/javascripts/users.js.coffee
      invoke    scss
   identical      app/assets/stylesheets/users.css.scss
      invoke  scss
   identical    app/assets/stylesheets/scaffolds.css.scss
5
  • 2
    Note: if you want the generated views to have attributes you need to include them after your model e.g User name:string email:string etc.
    – skalb
    Oct 7, 2013 at 18:42
  • What does the '--migration=false' do?
    – Kevin Zhao
    Sep 2, 2015 at 0:15
  • 1
    @KevinZhao ... Doesn't generate migrations. Sep 2, 2015 at 0:19
  • 1
    This does not really help. Rick Smith answer below is correct.
    – CppNoob
    Jul 19, 2017 at 18:45
  • 1
    @CppNoob Turns out Rails actually changed over the four years between the original answers and the new ones from 2015. Jul 19, 2017 at 18:51
31

This is what the scaffold generator calls internally:

rails g erb:scaffold User

erb is the templating engine used, so you can also use haml:scaffold.

You must explicitly specify the fields you would like the scaffolding to use--rails does not automatically deduce them from the created model. For example:

rails g erb:scaffold User firstname lastname reputation

See rails g --help for options like skipping, forcing overwriting, and dry runs or generate scaffold --help for information specific to generating scaffolding.

1
  • 2
    I feel like this is actually the correct answer. All other options will generate whatever is missing. This one only generates the views. Thank you :) Dec 13, 2015 at 13:10
2

I just encounter the same your problem. I did it. More detail is below:
- First I rename views/your_model folder to views/your_model_bak. In order to revert if fail later
- Then, execute command

rails g scaffold YourModel [field[:type][:index]] --skip
  • Don't forget --skip option, it will not create exist files (controller and model in this case and few other files)
  • Make sure list [field[:type][:index]] is up to date

-- Finally, you should update your permit in your_model controller.

Hope it can help you.

2

"Another tool"...

How about being able to do "script/generate view_for model_name"? :)

There is a gem for that - View Mapper. It has Ruby on Rails 2 and 3 versions.

1
  • 2
    yeah that was from 2011, now 4 years old. Apr 9, 2015 at 11:43
1

One small tip is to add "--no-test-framework" if using Rspec and don't want test files generated for each view in spec/views

-2

To generate views after controller and models are already created, you may use the command line. You switch to the folder in which you want to create the new view. For example:

$ cd name_app/app/views/controller_name
$ touch name_file

To go back of one directory use:

$ cd ..
1
  • 1
    ...? Or just create the file in an editor. I don't see how this is particularly helpfile, it's basically saying "create the file by creating the file", and it doesn't create all the associated Rails-ish files, just whichever one you created on the command line. Sep 2, 2015 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.