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?
Related
I have a very large model (say 200 fields), so it isn't very convenient to write them down into the command line.
So I first generate the migration then i did scaffold with:
rails generate scaffold myModel --migrate=false
It generated the controller with its actions, it updated the routes.rb and created views/myModel.
But it didn't add the fields to the views. Isn't scaffold supposed to provide a basic field presentation? If not, is there a way to provide it?
Scaffold uses the command line arguments you specify to create the relevant files. If you don't specify any fields at the command line then it can't add them to the views (because it doesn't know them). The Scaffold command is a one time thing. You might consider typing out and/or programmatically creating the relevant scaffold command in a text editor and pasting it into the console.
rails generate migration
rails generate model
rails generate scaffold
rails generate controller etc.
How these differ?
According to rails guides:
Using generators will save you a large amount of time by writing boilerplate code, code that is necessary for the app to work, but not necessary for you to spend time writing. That’s what we have computers for.
rails generate commands family used to provide simple and easy way for developer to create different object types.
rails generate migration - creates DB migration script in db/migrations directory so developer can setup his DB.
rails generate model - creates model class with associated migration, test and fixtures (test data).
rails generate scaffold - creates all nedded classes with basic logic and presentaion. It creates controller (with simple CRUD logic), model, fixtures, functional and unit tests.
rails generate controller - creates controller with associated functional tests, helper and basic views templates.
You can read more here: http://guides.rubyonrails.org/command_line.html#rails-generate
They differ in the sense that they generate different stuff.
migration will generate a database migration file,
model will generate a model(with a migration and a spec by default)
scaffold will generate a scaffold of a resource
and controller will generate a controller.
generate means it will create the files for you with boiler plate code already in place(you will still need to edit them though..but scaffold can get you working with a basic application already)
Read more about it here: http://guides.rubyonrails.org/command_line.html#rails-generate
rails generate is a command line script for quickly generating the code for various Rails' constructs.
In the example you give they differ by what they produce, with the first argument being the type of code generated. For example if I wanted to create a User model I would run:
`rails generate model user`
The model file, test file and migration would be created for me.
You should read the Rails' documentation to find more.
**rails generate model user:
The above command create a Template Object that is a mirror image of the database table.
For example, if you have a database table that is named users that has a name:string, and email:string field,then "rails generate model user" create an Object that mirrors that user table with a few addition.
Here are the similarity they both have name:string,email:string the both have the word user.
The difference are few but significant: user is Capitalized in the model name like "User".
The Table add create_by and updated_by automatically.
migration:db create the database mirror using the model as a model.RECURSION ANYONE?
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
I have a question to the following procedure:
script/generate scaffold product
title:string description:text
db:migrate
then I generate a migration which adds a column description to the
table products and migrate the db
again.
My question is: why is the field description not added to the project-views? Is that normal rails scaffold behaviour?
I think I saw in a video tutorial that the scaffold updates as well the views, which would be very convenient.
Thanks in advance for any help!
This is normal since scaffold does not "monitor" changes to the table or to any other scaffold-related resource (controller, model, views, tests, etc.).
then I generate a migration which
adds a column description to the table
products
You have description column already when generating the scaffold. Why do you need another migration for this?
This is normal behaviour for scaffolding, however there are alternatives.
If you were to look at Ryan Bates nifty_scaffold generator then this would allow you to re-run the scaffold generation. Assuming that you hadn't changed the generated code.
These generators can regenerate the views/spec/tests based upon the current state of the database model.
Be aware that if you have already customised the views yourself then they could be overwritten.
I have a database with tables. I want to create a model in my Rails app from existing table. As i know, such functionality is available, and is done as follows:
script/generate scaffold model_name --skip-migration
Of course, i defined my database in database.yml file. Scaffold generated for me a model with controller and views. My table name is not as it must be for Rails(it is incorrect, not following conventions), i added set_table_name to my controller. But, when i am calling the index method, on my page i have only set of # symbols, but not a data from database. In my index.html.erb i have only generated code by scaffold. How can i print out my database data?
Have you generated a schema file from your existing database? If you run the command
rake db:schema:dump
and then re-generate your scaffold this should fix the problem.
Additionally you may wish to check out Dr Nic's Magic Model generator. This will generate models for all of your existing tables and attempt to guess the relationships. This will probably not work if your table naming is not understandable by rails.
UPDATE
I do not generally use the default scaffold however I have tested this myself and it appears that if you skip the migration and do not pass any column name/type pairs then the scaffold generator will not create anything in the template to render the columns.
You have two choices here either
Pass in the column name pairs as well as skip-migration or
Download Ryan Bates Nifty Scaffold generator which will create the scaffold with the column names even if you specify --skip-migration