I'm new to Rails so my current project is in a weird state.
One of the first things I generated was a "Movie" model. I then started defining it in more detail, added a few methods, etc.
I now realize I should have generated it with rails generate scaffold to hook up things like the routing, views, controller, etc.
I tried to generate the scaffolding but I got an error saying a migration file with the same name already exists.
What's the best way for me to create scaffolding for my "Movie" now? (using rails 3)
TL;DR: rails g scaffold_controller <name>
Even though you already have a model, you can still generate the necessary controller and migration files by using the rails generate option. If you run rails generate -h you can see all of the options available to you.
Rails:
controller
generator
helper
integration_test
mailer
migration
model
observer
performance_test
plugin
resource
scaffold
scaffold_controller
session_migration
stylesheets
If you'd like to generate a controller scaffold for your model, see scaffold_controller. Just for clarity, here's the description on that:
Stubs out a scaffolded controller and its views. Pass the model name,
either CamelCased or under_scored, and a list of views as arguments.
The controller name is retrieved as a pluralized version of the model
name.
To create a controller within a module, specify the model name as a
path like 'parent_module/controller_name'.
This generates a controller class in app/controllers and invokes helper,
template engine and test framework generators.
To create your resource, you'd use the resource generator, and to create a migration, you can also see the migration generator (see, there's a pattern to all of this madness). These provide options to create the missing files to build a resource. Alternatively you can just run rails generate scaffold with the --skip option to skip any files which exist :)
I recommend spending some time looking at the options inside of the generators. They're something I don't feel are documented extremely well in books and such, but they're very handy.
Great answer by Lee Jarvis, this is just the command e.g; we already have an existing model called User:
rails g scaffold_controller User
For the ones starting a rails app with existing database there is a cool gem called schema_to_scaffold to generate a scaffold script.
it outputs:
rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string
from your schema.rb our your renamed schema.rb. Check it
In Rails 5, you can still run
$rails generate scaffold movie --skip
to create all the missing scaffold files or
rails generate scaffold_controller Movie
to create the controller and view only.
For a better explanation check out rails scaffold
This command should do the trick:
$ rails g scaffold movie --skip
You can make use of scaffold_controller and remember to pass the attributes of the model, or scaffold will be generated without the attributes.
rails g scaffold_controller User name email
# or
rails g scaffold_controller User name:string email:string
This command will generate following files:
create app/controllers/users_controller.rb
invoke haml
create app/views/users
create app/views/users/index.html.haml
create app/views/users/edit.html.haml
create app/views/users/show.html.haml
create app/views/users/new.html.haml
create app/views/users/_form.html.haml
invoke test_unit
create test/controllers/users_controller_test.rb
invoke helper
create app/helpers/users_helper.rb
invoke test_unit
invoke jbuilder
create app/views/users/index.json.jbuilder
create app/views/users/show.json.jbuilder
I had this challenge when working on a Rails 6 API application in Ubuntu 20.04.
I had already existing models, and I needed to generate corresponding controllers for the models and also add their allowed attributes in the controller params.
Here's how I did it:
I used the rails generate scaffold_controller to get it done.
I simply ran the following commands:
rails generate scaffold_controller School name:string logo:json motto:text address:text
rails generate scaffold_controller Program name:string logo:json school:references
This generated the corresponding controllers for the models and also added their allowed attributes in the controller params, including the foreign key attributes.
create app/controllers/schools_controller.rb
invoke test_unit
create test/controllers/schools_controller_test.rb
create app/controllers/programs_controller.rb
invoke test_unit
create test/controllers/programs_controller_test.rb
That's all.
I hope this helps
Related
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.
I have generated two scaffold using the following two commands:
$:- rails generate scaffold User user:string gender:string
$:- rails generate scaffold Microposts microposts:string
Is there any command that will list me the different scaffolds i have generated so far?
It should give me the output similar to this:
Scaffold generated:
User
Microposts
Not that I know of, but you can see the direct result in your application, plus after each scaffold you see which files are generated. To quote RoR guides:
A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
A scaffold in itself is not one thing, it's a collection. What would be the point of only displaying the name you used to scaffold all these files?
I don't think so - scaffolding generates models, views and controllers for a given class (in your case User and Microposts) - if you look at app/models you'll see User.rb and Microposts.rb. So maybe that's nearly the same thing?
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 ..
I am trying to generate a controller with all the RESTful actions stubbed. I had read at Wikibooks - Ruby on Rails that all I needed to do was to call the generator with the controller name and I would get just that. So, I ran script/generate rspec_controller Properties but got an empty controller.
Any other suggestions would be greatly appreciated.
I don't know about an automated way of doing it, but if you do:
script/generate controller your_model_name_in_plural new create update edit destroy index show
All of them will be created for you
Update for Rails 4
rails g scaffold_controller Property
In Rails 3 there is also rails generate scaffold_controller .... More info here.
EDIT(due to some comments) : Original question was in 2010 - hence the answer is NOT for RAILS 4 , but for rails 2!!
try using scaffolding.
script/generate scaffold controller Properties
Section of Official docs on Ruby On Rails
I'm sure you can find more info if you do a google search on rails scaffolding. Hope that helps.
EDIT:
For RAILS 4
rails g scaffold_controller Property
In Rails 4/5 the following command does the trick for me.
rails g scaffold_controller Property --skip-template-engine
It generated the controller actions but not the view.
Rails 5.1
Starting point:
You have created a model without a controller, nor views (eg thru: rails generate model category)
Objective:
Upgrade it to a full RESTful resource
Command:
rails generate scaffold_controller category
It stubs out a scaffolded controller, its seven RESTful actions and related views. (Note: You can either pass the model name CamelCased or under_scored.)
Output:
varus#septimusSrv16DEV4:~/railsapps/dblirish$ rails generate scaffold_controller category
Running via Spring preloader in process 45681
create app/controllers/categories_controller.rb
invoke erb
create app/views/categories
create app/views/categories/index.html.erb
create app/views/categories/edit.html.erb
create app/views/categories/show.html.erb
create app/views/categories/new.html.erb
create app/views/categories/_form.html.erb
invoke test_unit
create test/controllers/categories_controller_test.rb
invoke helper
create app/helpers/categories_helper.rb
invoke test_unit
invoke jbuilder
create app/views/categories/index.json.jbuilder
create app/views/categories/show.json.jbuilder
create app/views/categories/_category.json.jbuilder
You're looking for scaffolding.
Try:
script/generate scaffold Property
This will give you a controller, a model, a migration and related tests. You can skip the migration with the option --skip-migration. If you don't want the others, you'll have to delete them yourself. Don't worry about overwriting existing files, that won't happen unless you use --force.
As klew points out in the comments, this also defines the method bodies for you, not just the names. It is very helpful to use as a starting point for your REST controller.
In Rails 4 it's rails g controller apps new create update edit destroy show index
Or rails generate controller apps new create update edit destroy show index if you want to write out the full term :).
script/generate rspec_scaffold Property
There's no way (that I know of? that is documented?) to stub out a controller except through scaffolding. But you could do:
script/generate controller WhateverController new create edit update destroy show
One solution is to create a script that accepts one parameter, the controller name, and let the script type the whole command for you.
Create a new file, say, railsgcontroller
Make it executable and save it on path
Run it like:
$ railsgcontroller Articles
die () {
echo "Please supply new rails controller name to generate."
echo >&2 "$#"
exit 1
}
[ "$#" -eq 1 ] || die "1 argument required, $# provided"
rails g controller "$1" new create update edit destroy show index