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.
Related
Each time I generate a scaffold I need to customize the stylesheet. So it takes much more time to update style for index, form and show page. If I paste the predefined template code then I will have to change rails code instance variables and path.
Is there any way to define a specific style for each and every scaffold so that I used this to save time and overhead.
In short, I want to layout my page generate from scaffold like my form, index and show page with a predefined style.
I want to use custom mark-up and stylesheet in the generated scaffold. I want to do a one time definition of this scaffold template and use it every time I generate a new scaffold.
I partially solved my problem though .
rails g bootstrap:layout [LAYOUT_NAME]
and
rails g bootstrap:themed [RESOURCE_NAME]
generate a scaffold by this command
rails g scaffold product name:string description:text
then implement bootstrap theme on your scaffold through this command
rails g bootstrap:themed products -f
pass the -f option to force it to overwrite the generated view files
you can generate a layout through command.
rails g bootstrap:layout appstructure
this file will be save in view/layout and reflect on each and every page.
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?
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?
I'm new to RoR, and I've just used scaffold to generate a table and create the pages for CRUD operations. Now I want to add a new field to this. One place I found tells me how to do that in the database, but is there a way to do it where it will add the field to all the pages too, or is that just a manual operation and I need to make sure I know all my fields up front?
To add a new column to the database
$ script/generate migration add_fieldname_to_tablename fieldname:string
$ rake db:migrate
To get your views up to date you can run the scaffold again, with your updated list of fields. It will balk on replacing your migrations but you can force it to replace your views.
$ script/generate scaffold tablename fieldname:string old_field_1:string ...
At the prompt answer a and it will overwrite the views, but not the old migration. It also won't modify your existing data.
First you'll write a migration to add the field, run the migration, then you need to rerun the scaffold to regenerate the views, etc. Beware, this will wipe out edited files from before. Of course, instead of scaffolding again you could manually add references to new field where appropriate.
You will have to update your database, no matter what (Remember to 'rake db:migrate' after you create the migration!)
Regarding the interface, you are a bit more lucky: the formtastic plugin makes your views look like this:
The 'f.inputs' is calculating the form fields on-the-fly, based on your model's attributes. This will not cover complex forms that need special treatment, but the usual ones, you will get them automatically.
For an easy-to-understand tutorial, see the latest railcast (Railscast #184, you will have to google for it, I can't post 2 links because I'm not cool enough for stackoverflow yet, sorry).
Railcast #185 is supposed to continue covering formtastic, and it's due to be published next monday.
Needs to be done manually, or the scaffold needs to be regenerated.
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