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

I got a project that has controllers (only minimal code) and models, but no views. Is there a way to generate views only with a scaffold or other tool?

+35
ruby-on-rails views models
Nov 13 '11 at 21:26
source share
6 answers
rails g scaffold User --migration=false --skip 

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

If you do not 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 
+74
Nov 13 2018-11-21
source share

This is what the internal sounder generator calls inside:

 rails g erb:scaffold User 

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

You must explicitly specify the fields that you want to use to use the forests: rails does not automatically derive them from the created model. For example:

 rails g erb:scaffold User firstname lastname reputation 

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

+12
Oct. 06 '15 at 0:03
source share

"Another tool" ...

What about the possibility of " script/generate view_for model_name "? :)

There is a pearl for this - View Mapper . It has versions of Ruby on Rails 2 and 3.

+2
Nov 13 2018-11-21T00:
source share

I just run into the same problem. I have done it. More details below:
- First, I renamed the views / your_model folder to views / your_model_bak. To return if an error occurs later - Then run the command

 rails g scaffold YourModel [field[:type][:index]] --skip 
  • Do not forget that the -skip option will not create existing files (the controller and model in this case and several other files)
  • Make sure the list [field [: type] [: index]] is updated

- Finally, you must update your permission in your_model controller.

Hope this helps you.

+2
May 24 '15 at 4:06
source share

One little --no-test-framework advice is to add the --no-test-framework when using Rspec and you don’t want the test files generated for each view in spec / views

+1
May 28 '13 at 16:51
source share

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

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

To return to a single directory, use:

 $ cd .. 
-one
Aug 07 '15 at 17:59 on
source share



All Articles