I have just created the project on ruby on rails. I also generated a controller with an instruction on bash, "rails g controller home".
However, I didn't make database. How can I generate database? Please inform me how to make database.
Thank you.
You can generate a database table by typing:
rails g model myTableName
Because your starting out new i think it would be best to try something like:
rails g scaffold Post name:string content:text
This will link the controller, the model(which is the database) and the views together, giving you a better understanding on how the MVC structure works in rails.
I would like to create a model called models/thing.rb, a controller called controllers/things_controller.rb, and views called views/things/index.html.erb and views/things/hello.html.erb.
How do I do this from the rails console? I know I have to use rails generate.
Firstly generate the controller with two actions index and hello.
rails g controller things index hello
This command will generate the controller and the views with views/things/index.html.erb and views/things/hello.html.erb.
Then generate the model:
rails g model Thing attribute:type [...]
If you want to know more about rails generate command, you can take a look at A Guide to The Rails Command Line
I'm new to rails trying to modify a model generated by scaffolding in rails 3. It seems like the old way is to use a script/generate command:
ruby script/generate migration add_fieldname_to_tablename fieldname:string //old way??
But when I try the old command it no longer works.
Since you longer use the ruby script/generate command to create a model I'm assuming the syntax to modify a model has changed also. What command should I use ?
Use rails generate migration/model/scaffold.
Using your example: rails generate migration add_fieldname_to_tablename fieldname:string
Is there a way to generate a subset of methods in the controller using scaffold instead of the regular scaffold?
For example: Only create new and show?
I am using rails 3, ruby 1.9.2
Thanks
I do not think the default rails scaffold generator supports this in Rails 3.
However, this can be accomplished using the nifty-generators gem:
https://github.com/ryanb/nifty-generators
In plain Rails3, you can do this when you generate the controller:
rails generate controller Artists new create
So, you could just generate the model, and then the controller, and get the overall functionality you were looking for.
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