Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Ruby on Rails views (only) after controllers and models are already created

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?

like image 378
Sean Avatar asked Nov 13 '11 21:11

Sean


2 Answers

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
like image 74
Dave Newton Avatar answered Oct 06 '22 06:10

Dave Newton


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.

like image 40
Rick Smith Avatar answered Oct 06 '22 06:10

Rick Smith