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

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)

Related

Should the input form be updated automatically after making associations to request for value of the foreign key?

I generated two Active record models called User and Job.
I made an association between the two where:
User belongs_to :Job
Job has_many:User
I can confirm this worked since I can see a foreign id column (job_id) in the users table. However, when ever I go to http://localhost:3000/users/new to create a new user record, the only Inputs form I see are Name and Email (which are the other columns I have on the users table) and I can not see any Job input form to enter value for job id. Whenever I try to create a new user record I get an error message saying 1 error prohibited this user from being saved:Job must exist
Is this how the Active record model should be like when I make a new association to a model? Do I have to manually change the user_params inside users_controller.rb or do I change somewhere else?
I'm asking this question because the tutorial I was following made it look as if the form should automatically change to include the foreign keys.
I was able to achieve what I wanted by using generate scaffold_controller and regenerated the entire controller and view whilst keeping the model the same.
In my case I run
rails generate scaffold_controller user name:string email:string job_id:int

Cannot decide Model Associations in rails

I am new to rails and having a hard time figuring out what is the best association I can create for my models. The models I have are:
User
Article
List
ArticleLink
ArticleMeta
etc..
Here, User can create many Articles and Lists, an Article and List can belong to multiple Lists, an Article can have multiple ArticleLinks and ArticleMeta. I also want to delete all the references of a model wrt User when it is deleted. For example when an User deletes a List, all the reference of the List should be deleted from join tables.
Also please let me know what would be the most efficient way to insert records in each model and retrive records as well
Thank you for your help.
You can find all your answers over this link
http://guides.rubyonrails.org/command_line.html
As example
rails generate scaffold Articles name:string category:string lists:references
or
rails generate scaffold ArticleLinks name:string Article:references

How to generate scaffold files using Rail's generate on Cloud9

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.

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.

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.

Resources