I am running
ruby script/generate scaffold
or
ruby script/generate model
and I know the basic syntax, like
ruby script/generate scaffold Dude name:string face:boolean
but I do not know things like:
should names of variables have underscores or be camelCased?
what kind of variable types are acceptable?
Where can I find such information?
Type the command without arguments and the documentation is revealed:
$ script/generate model
You can use either camelcase or underscores for the model name.
Model names are singular; controller names are plural.
Field names use underscores.
I can't remember all the possible field types, I just look them up from the Migration docs, as linked above.
This document on Rails Migration would help.
With respect to the naming convention, I think the general adopted convention for Ruby on Rails is to have underscores.
To know which variable types are acceptable, refer to the section on Database Mapping.
There is a resource on the rails wiki as a List of Available Generators.
To check Rails naming conventions, topfunky's Pluralizer was useful.
there is a new syntaxis for Rails is rails generate
Related
I have run a migration and am getting a 500 error saying seen_by_hw does not exist. Could this be due to the fact I have used an uppercase characters when creating the ruby model. eg
seen_by_HW. Or should it be seen_by_hw
thanks
If you created the model file/class yourself then yes, this is probably the issue.
The filename should be: seen_by_hw.rb and in that the class name will be SeenByHw.
If you use the generators that comes with Rails then it would have done this for you, type rails generate in your app directory to see more info on them.
I have a model called MyArticle. When I try to use the command
rails generate migration AddtestToMyArticle test:string
the migration file contains only empty up/down methods. Having done this previously on a single word model name, it worked just fine and the migration up/down methods had the appropriate code.
I tried "AddtestToMy_Article" but that didn't work either. What do I need to do to work with my compound model name and the generate migration command?
You need to use
rails generate migration AddColumnToMyArticle test:string
When using 'AddColumn' you will have the appropriate code in your migration.
I just looked back over this, and while my answer is correct, it's better to have a more descriptive migration name. The user below who noticed the capitalization is right in that if you don't have each new word capitalized, Rails won't pick up on what exactly you're trying to do. So, in your question you have AddtestTo... but it should be AddTestTo....
It seems to work if you use underscores rather than CamelCase
rails g migration add_test_to_my_article test:string
Hope this helps
rails generate migration AddNewFieldToMyArticle new_field:string
I've started learning Ruby last week, and I've started learning Rails today, so bear with me.
I see that Rails come with generators that allow you to generate models/controllers or even model+controller+views bundle as 'scaffold'. This is what I am interested in.
However, I have a question. How do I set database options of the column?
eg. To generate a users table I would do:
rails g scaffold users uuid:binary name:string email:string password:binary registered_on:date number:integer default:string
Now what if I'm integrity freak and am not happy by having validation just in model/controller, but want to do database level limitations as well.
What if I want email to be 50 characters max, and number to Auto-Increment and neither of all fields is allowed to be NULL and default field must have a default of 'foo'. Is there any way to pass these requirements to generator command?
I know its possible to set these options in .rb file that is used in rake db:migrate, however it would be easier to just pass values in with 1 command.
At least some things are available, like string length, but not sure about the others.
rails g scaffold users email:string{50}
Use type modifiers between curly braces, example:
rails generate migration AddDetailsToProducts price:decimal{5,2}
More info: migrations
Use scaffold and obtain generic migration files. And, as you mentioned, do the customizations in there. This files are found in db/migrate.
After you are done customizing the fields, don't forget to do a rake db:migrate.
I have an app that's heavily form-based. Many of the forms are constructed exactly alike so it seems like a natural fit for a generator. What I want to do is create one that works like this (fictitious example):
rails g request_form name:string phone:string date_of_birth:date
In any case, the standard, empty controllers, helpers, models, and so on won't quite do. I've read the Rails code but it's, frankly, not been a heck of a lot of help in puzzling this out. What I want to do specifically is:
Create a model and migration given the fields specified on the command line
Create a controller and helper, based on my template
Create views based on my templates
Create empty specs
Create a mailer based on my template
Create mailer views based on my templates
I'm getting stalled at square 1: How the heck do I get the ARGV part of the rails g command -- that is, the field names? Then there's square 2: How do I hook into the built-in generators, where appropriate and fill in my own stuff where not?
This is analogous to
rails g scaffold blah:type blah1:type
so I don't think this is biting off more than I can chew...
Any help mucho appreciated!
All inspiration needed in this great gem: https://github.com/ryanb/nifty-generators
The awesome Ryan Bates has a screencast on writing generators in Rails 3, have you watched that?
Say I have the model CourseGroup. What would be the controller's name?
The controller name would be course_groups_controller.
http://itsignals.cascadia.com.au/?p=7
To find the name for any model, you can open up a rails console and do "ModelName".tableize. Then just add "_controller" to the end. This would result in model_names_controller.
Here's an easy way to find out the naming conventions: Just create a throw-away Rails app in a temp directory, with a scaffolded model:
rails blog
cd blog
./script/generate scaffold post subject:string content:text
You can then browse through the files and directories to see how things are named. I like to keep one of these around just to refer to from time to time. And by the way, running the generators without any parameters gives help output which includes examples of naming conventions:
./script/generate scaffold