How to generate scaffold files using Rail's generate on Cloud9 - ruby-on-rails

I am creating a site that will sell phones and I have tables and products all set up.
When I run:
rails generate Scaffold Sale item_id:integer employee_id:integer
I get a Ruby file called 'Sale' and 'Part.rb' in my app/models. I am not getting the two Ruby files I need, which are 'Emlployee.rb' and 'Item.rb'.
I need these files in order to keep track of which employee sold which phone. When I navigate to my site /sales there is a table but when I navigate to /employees to add a new employee Cloud0 tells me path is unspecified.
What am I doing wrong?

rails generate scaffold Sale item_id:integer employee_id:integer
Only creates the stuff that belongs to the Sale. So, in your case with this command you created a Sale.rb model that has two attributes - item_id and employee_id (scaffold creates also it's assets, controller, routes and tests).
If you need to create also Employee and Item models, you'll need to create them separately.
> rails g model Employee
> rails g model Item
Read also the difference between scaffold and model in Rails and the documentation about the generate command.

Related

Is there a way to set up model associations when you generate a migration?

New to rails and wondering if there is a way to have all the associations you generate from a migration show up in that model. For example, working on a mock AirBNB app. If i were to generate a migration like so (with these models already created)
rails g migration AddListingsToNeighborhood listing:belongs_to
When I run rake db:migrate, my models are still empty. Just wondering if there is a shortcut to have these associations fill the models.
Thank you!
If you have not created the model (or can overwrite it), use
rails g model Neighborhood listing:references
If you have the model and just need to create the appropriate migration:
rails g migration AddListingsToNeighborhoods listing:references
The DB migration will not modify the model.
In these examples, Neighborhood gets the foreign key.
If you want Listing to have the foreign key (and have belongs_to), then you would need to reverse them:
rails g model Listing neighborhood:references
rails g migration AddNeighborhoodToListings neighborhood:references
FWIW, as you grow your apps, you will often be modifying existing models and adding relationships. This means manually adding belongs_to and running the migration.

Generating a scaffold with existing database

i want to generate a scaffold to an object which already has table in the database, so i don't need to migrate and stuff, but the question is, when i am generating a scaffold, for example:
rails generate scaffold Product title:string description:text image_url:string price:decimal
do i still to specify those fields ? (title, description, etc'..) or those fields are only used for the migration part, so if i already have a table, i don't need to specify them ..
thanks!
Generator is reading the fields you supplied and is building all the views (including forms) based on the input. It does not read the current db schema, so yes, you have to supply all the attributes.
UPDATE:
Note that it wills still work without attributes, but it will generate empty views.

What's the proper way to create a database referencing a user using the devise gem?

I'm creating a simple webapp that tracks calories as a way to learn Ruby on Rails. I'm using the gem devise for users to sign in. My next step is to generate a model. I was going to use:
rails generate model Tracker calories:integer consumed_on:datetime
The problem though is that I don't know how to relate this data to the signed in user. What am I missing from this generate model command?
rails generate model Tracker user_id:integer calories:integer consumed_on:datetime
This will generate your model with a foreign key in the database referencing the user table.
Of course this is not all you have to do, you have to put other code in your model and controller to combine those two columns, create the views, update the config/routes.rb file ..
Also:
Don't forget to run the rake db:migrate to make the relational table in the database.
You can see how its done here (your Tracker model/table is theirs Micropost):
http://ruby.railstutorial.org/chapters/a-demo-app#sec-microposts_resource

Rails - generate scaffold for multi select

I used the following method to generate scaffold for platform list in our app:
rails generate scaffold platform name:string url:string country:string
I would like to still use scaffold possibility to add additional attributes, like type and gender, which should be drop downs, or preferably multi-select's.
Is there a way to use rails generate to create such a models?
You have already generated a scaffold for Platform. So you have two options:
edit scaffold by hand to add the new fields (recommended)
remove scaffold with rails destroy scaffold and then use rails generate again
Anyway scaffolding doesn't allow you to specify options for a drop-down menu. In any case you have to manually edit the scaffold. Furthermore in Rails migrations you cannot specify ENUM fields, so the best option for gender would be a string field with unitary length (with a validates_inclusion_of in model).
P.S. Don't use type as a column name because you will probably get strange behaviors.

ruby on rails - create and populate combobox / dropdown from another scaffold

I want to display a combobox in my web page.
I have two objects :
i.e.
rails generate scaffold person id:integer name:string
rails generate scaffold state id:integer name:string
I want on the person to be able to choose state.
What is the best practice?
should I run a different scaffold ?
(one that will create me a foreign key in the db and another code ?)
thanks.
If a Person can only be linked to a single State at any time, add a state_id field to Person. If a Person can have many States linked to it, for example, Residences you should create a object for that containing both the state_id and the person_id.
More on relations and linking models can be found in the Rails Guides
To render a dropdown list of states you can use the select helper method. Make sure to use the proper field name for the select input field(state_id), instead of the name of the association.(state)

Resources