Rails - generate scaffold for multi select - ruby-on-rails

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.

Related

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.

How do I create a model with many fields on rails? Two fields should have options to choose from

I want to create a model with many fields on rails. I want two of the ten fields to have two or three options to choose from to check off.
The guide I'm using has only two fields, as follows:
rails generate scaffold topic title:string description: text.
It seems like the easy way out is to do a migration in this format:
rails generate migration AddClosing_Hrs1ToBusinesses closing_hrs1:string new_cloumn:string third_column:string
(source: How to add several columns to a database in Rails).
Any advice?
It depends on where you are in the process.
If you have not yet created the model at all you can just add more fields to the original generate scaffold line. For example if you wanted 4 fields:
rails generate scaffold topic title:string description:text another_item:string some_number:integer
If you already created the model but have not yet run rake db:migrate then you can find the migration file in app_name/db/migrate/. Open the file and add lines for your new fields
If you have already run db:migrate then you should follow the answer for "How to add several columns to a database in Rails" that you linked in your answer.
For the fields where you want several options to select from those options will need to be handled by a combination of your model and view code. Assuming the options will stay constant and you can only select one first define the options array in your model using something like:
OPTIONS_FOR_TITLE = ["Title 1", "Title 2", "Title 3"]
Then in the view code you will use the select and options_for_select helpers to create the view. More details on them are available here:
http://guides.rubyonrails.org/form_helpers.html#the-select-and-option-tags
For lots of detailed information about migrations:
http://guides.rubyonrails.org/migrations.html

Any Rails scaffold data types that generate radio buttons in the _form.html.erb?

For instructional purposes, I'd like to generate scaffolds which showcase radio buttons. You can generate a scaffolding with checkboxes with:
rails generate scaffold Person name:string honest:boolean
boolean data types create checkboxes for the _form.html.erb file used when creating or editing people above. Is there anything that creates radio buttons?
No, not using the default scaffold generator. Each attribute type (integer, string, boolean etc) defaults to one field type. See rails/generators/generated_attributes.rb for how it is implemented.
If you still want to automatically create radio-buttons for the boolean values, you may have to override the default scaffold generator. Here's some documentation on how to do that, though I recommend that you change your check_box fields to radio_button fields manually.

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