I've changed my models significantly and my unit tests and fixtures no longer make sense. Is there a quick rails generate command I can use to "regenerate" the test directory?
There's good news to regenerating files! If one deletes the files one wants to re-generate and then run
rails generate scaffold Model [attributes] --skip
for each model affected, only the deleted files will be regenerated. Hopefully this helps everyone else who has slacked off on updating the basic unit tests throughout many model changes and will like to quickly generate them to match their current model.
rails generate scaffold User name email password_digest is_admin:boolean --skip
invoke active_record
skip db/migrate/20140219190354_create_users.rb
skip app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
invoke resource_route
route resources :users
invoke scaffold_controller
skip app/controllers/users_controller.rb
invoke erb
exist app/views/users
skip app/views/users/index.html.erb
identical app/views/users/edit.html.erb
skip app/views/users/show.html.erb
skip app/views/users/new.html.erb
skip app/views/users/_form.html.erb
invoke test_unit
create test/controllers/users_controller_test.rb
invoke helper
identical app/helpers/users_helper.rb
invoke test_unit
create test/helpers/users_helper_test.rb
invoke jbuilder
skip app/views/users/index.json.jbuilder
skip app/views/users/show.json.jbuilder
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
Now onto writing real tests =)
I think you're SOL, but if you've been using rails generate throughout your process hopefully your directory won't be too far off.
Related
I am going to removing existing model form my application, then its works fine.
rails d model student
gives:
invoke active_record
remove db/migrate/20170803044626_create_students.rb
remove app/models/student.rb
invoke test_unit
remove test/models/student_test.rb
remove test/fixtures/students.yml
when I am going to remove non-exist model like
rails d model test
gives:
invoke active_record
remove db/migrate/20170803044857_create_tests.rb
remove app/models/test.rb
invoke test_unit
remove test/models/test_test.rb
remove test/fixtures/tests.yml
Why is model removing? although the model does not exist.I expect an error.
I have created a new application of Ruby on Rails. I use RubyMine and created the project using New Project->RailsApps Sample->rails-devise-pundit
I have created models and controllers, but just realized I do not have any test directory under my root path.
For example, I have a Transaction model and when I do:
rails g resource Post --skip --no-resource-route --no-migration --no-helper --no-assets
I get this:
invoke active_record
skip app/models/transaction.rb
invoke rspec
identical spec/models/transaction_spec.rb
invoke factory_girl
identical spec/factories/transactions.rb
invoke controller
skip app/controllers/transactions_controller.rb
invoke erb
exist app/views/transactions
invoke rspec
See, there is no test directory at all here
How can I auto generate test directories with resources for all of my models and controllers? (As I see a guide for testing in Ruby here: http://guides.rubyonrails.org/testing.html)
When I deleted a controller I noticed that the corresponding routes are not deleted from the routes.rb file. Is there any reason for rails not to delete it?
The routes which belong to that controller won't be deleted when you give rails d controller Posts since they are not created when the command rails g controller Posts name:string user:references is run. The contents which are generated by rails g controller Posts name:string user:references are only deleted when you give rails d controller Posts
For example, the command rails g controller nurseries creates the below files
create app/controllers/nurseries_controller.rb
invoke erb
create app/views/nurseries
invoke test_unit
create test/controllers/nurseries_controller_test.rb
invoke helper
create app/helpers/nurseries_helper.rb
invoke test_unit
create test/helpers/nurseries_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/nurseries.js.coffee
invoke scss
create app/assets/stylesheets/nurseries.css.scss
Now when you give rails destroy controller nurseries the content which is created with the command is only deleted
remove app/controllers/nurseries_controller.rb
invoke erb
remove app/views/nurseries
invoke test_unit
remove test/controllers/nurseries_controller_test.rb
invoke helper
remove app/helpers/nurseries_helper.rb
invoke test_unit
remove test/helpers/nurseries_helper_test.rb
invoke assets
invoke coffee
remove app/assets/javascripts/nurseries.js.coffee
invoke scss
remove app/assets/stylesheets/nurseries.css.scss
May be you should manually erase the routes or if you are generated the controller with help of scaffold i.e, rails g scaffold Posts name:string user:references then giving rails d scaffold Posts will do it for you, but it also deletes all the other content(model,migration files etc) along with it.
Rails does not edit your files unless you call a command to do so (i.e. rails generate ...)
So when you delete a file from your hard disk with rm app/controller/foos_controller.rb there is no process which could edit your routes.rb file.
Is it possible to create controllers, models and view from the existing database?
I could not find the command over googling.
Here i am talking about Reverse Engineering
You have to create simple model for every table with relations, and then you can
[rails3] > rails generate scaffold_controller Club name:string exclusive:boolean
create app/controllers/clubs_controller.rb
invoke erb
create app/views/clubs
create app/views/clubs/index.html.erb
create app/views/clubs/edit.html.erb
create app/views/clubs/show.html.erb
create app/views/clubs/new.html.erb
create app/views/clubs/_form.html.erb
create app/views/layouts/clubs.html.erb
invoke test_unit
create test/functional/clubs_controller_test.rb
Alternatively you can try active_admin gem
ActiveAdmin - https://github.com/gregbell/active_admin
rails generate active_admin:resource [MyModelName]
RailsAdmin is also good enough https://github.com/sferik/rails_admin
You should specify at least 2 rules for your model if it doesn't use rails conventions.
Example
class Article < ActiveRecord::Base
self.table_name "tbl_articles"
self.primary_key "art_id"
end
Well this goes against principles. The better you have to do, if you want a quick bootstrap for your application is replicate the models you have on your database and use scaffolding.
Remember that Rails use a LOT of conventions, and if you decide not follow you'll have a lot of trouble.
Check this guide if you need help.
This is how you can do that -
Try:
rails g scaffold myscaffold
This will generate the files:
invoke active_record
create db/migrate/20130124100759_create_myscaffolds.rb
create app/models/myscaffold.rb
invoke test_unit
create test/unit/myscaffold_test.rb
create test/fixtures/myscaffolds.yml
route resources :myscaffolds
invoke scaffold_controller
create app/controllers/myscaffolds_controller.rb
invoke erb
create app/views/myscaffolds
create app/views/myscaffolds/index.html.erb
create app/views/myscaffolds/edit.html.erb
create app/views/myscaffolds/show.html.erb
create app/views/myscaffolds/new.html.erb
create app/views/myscaffolds/_form.html.erb
invoke test_unit
create test/functional/myscaffolds_controller_test.rb
invoke helper
create app/helpers/myscaffolds_helper.rb
invoke test_unit
create test/unit/helpers/myscaffolds_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/myscaffolds.js.coffee
invoke scss
create app/assets/stylesheets/myscaffolds.css.scss
invoke scss
identical app/assets/stylesheets/scaffolds.css.scss
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?
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
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.
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.
"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.
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
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 ..