I'm just figuring out my way around rails but I need a little help with the rails generate scaffold command.
Here's the command that I'd like to use
rails generate scaffold Expense user:??? name:string description:text
I'd like the description field to be nullable and the users field to be linked to another Model — in this case I'd like to create a foreign key to the Users. I'm using the devise authentication framework.
I've read that many RoR developers try and avoid the scaffolding method and opt for the manual approach instead but my web-app is quite simple and I've thought of going the scaffolding way.
Scaffolding only generates the migration that you then run. Once the file is generated simply crack open the generated migration and adjust any of the values you need specific constraints on. By default columns are set to null unless you specify otherwise e.g.:
create_table "slugs", :force => true do |t|
t.integer "sequence", :default => 1, :null => false
t.string "sluggable_type", :limit => 40
t.string "scope", :limit => 40
t.datetime "created_at"
end
This is the code generated by the friendly_id plugin as you can see they have specified that the sequence column cannot be null while the other fields have other constraints.
Related
I'm having some troubles with a project I'm working on. Be warned I consider myself very much a beginner/novice at all this still :)
To keep things short and sweet, I'm using Rails & active admin to build up an admin interface where i can perform CRUD operations on my database models, which is all working great. However I recently decided I wanted to add another field to one of my models, a "description" field, so generated a migration, ran rake db:migrate and updated my list of allowed params in my controller & active admin resource.
My problem is data is not saved for this new "description" field - wether its via creating a new entry or updating an existing one. I can see the output in the terminal confirms it is being filtered out by strong params; returning Unpermitted parameter: :Description However i am under the impression i have set up my strong params correctly, so I'm unsure if i have set up my permit params properly or what else i can do.
Using Rails 5.1.0 & will post code below.
class CellsController < InheritedResources::Base
def index
end
private
def cell_params
params.require(:cell).permit(:name, :description)
end
end
#database schema for my cell model
create_table "cells", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "Description"
end
#Active Admin resource
ActiveAdmin.register Cell do
permit_params :name, :description
end
Again, greatly appreciate any help as I'm sure I've overlooked something, happy to provide any other information that is required :)
Thankyou!
To me it looks like the description param is not accepted because the model only has a Description column (with a capitalised D). To fix that, either change each params.permit(:description) to params.permit(:Description) or just rename the column inside a new migration:
def change
rename_column :cells, :Description, :description
end
I recommend renaming the column as it will avoid any trouble with the column in the future.
Is there a way to use the $ rails generate model to explicitly not using the id since I want to create a joined table with foreign keys instead.
I am fully aware that we can do that by going to the migration table, as follow:
# Just an example to demonstrate <id: false>
create_table :my_table, id: false do |t|
t.string :key_column
t.string :value_column
end
StackOverflow Source
But is there a way to do so using the generator? And yes, I've checked the help option:
$ rails g model -h
but maybe I'm missing something.
I have a model called configurations which holds key value pairs in my database to store options for the website. The schema looks like this:
create_table "configuration", force: :cascade do |t|
t.string "key", limit: 255
t.string "value", limit: 255
end
I would like to create a page in the website, so as an admin, I can edit the values on the configurations without the need to enter the database and change them manually.
However some of the values are strings, some are integers and some are Boolean, and I would like that to be reflected in the page, so a particular key might have a drop down of true/false for a Boolean value or a text box for a string etc.
Would this be possible, and if so, how do I write a controller/form to implement this please?
You can do above by standard Scaffold
rails g scaffold Configuration key:string value:string
then execute rake db:migrate
& allow this page only for admin
Scaffold will generate rails standard model, view & controller for the same.
How do I add data to a table in Rails?
So far I have created a Rails app that pulls in data from an API. Next, I have ran the command
rails generate model order
and I have /db/migrate/timestamp_create_orders.rb
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.string :email, null: false
t.string :order_date, null: false
t.string :total_price, null: false
t.string :order_number, null: false
t.string :cust_name, null: false
t.string :zip_code, null: false
t.timestamps null: false
end
end
end
Next, I believe I need to run db:migrate and that will create the table.
My question is, how do I add data to this table? I want the user to visit the app, it pulls in the data, and stores it in this table.
I have found conflicting advice..
Should I just use the advice from here
Order.create(:email=>'fake#fake.com',:order_data=>"...
But other advise seems to say not to do this here and here. Though they are all pretty old
You do not create database entries in migrations, you usually create schema or specify changes in the schema in migration files. You use seeds for creating seed data in the database.
To create new data in database through rails you can use either create or new method but you need to save the data as mentioned in other posts in your links when you are using new method.
While creating or migrating a new database table, table row is not automatically added. You need to add them manually. One way to populate the newly created database table is using seeds.rb file which is located in your application db folder. You can add Faker gem to your application for creating fake table attribute elements. An example using faker:
(1..3).each do # it'll create 3 new order
Order.create(email: Faker::Internet.email, order_date: Faker::Date.between(2.days.ago, Date.today))
end
Then run rake db:seed in your project folder console.
If you have some validation in your order.rb file, then you can create new instance of that order and then save it like:
order = Order.new(....)
order.save(validate: false)
I am creating a Ruby on Rails Application where I want to create a CRUD interface that will allow me to store records in a postgresql database. Eventually this will become part of the backend for an iOS game I am developing. I want to store arrays in these records of text and/or integer types respectively, but I don't know how...
So far I can create a CRUD interface via terminal, for example using a 'POST' model with the scaffold command like..
$ bin/rails generate scaffold post first_name:text last_name:text date_of_birth:date
This will produce a database migration file that looks like
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.text :first_name
t.text :last_name
t.date :date_of_birth
t.timestamps null: false
end
end
end
To best illustrate what I want to achieve, I have created the following example desired database migration file.
class CreatePosts < ActiveRecord::Migration
def change
create_table :player do |t|
t.text :player_name, null: false
t.text :player_email, null: false
t.integer :tokens_collected_by_level, array: true, default: []
t.text :items_collected, array: true, default: []
end
end
end
In order to help me understand how to create a CRUD interface for a Ruby on Rails App USING arrays, via Terminal.. What would be the corresponding 'bin/rails generate scaffold post ... ' command to execute, to generate the above code?
These are the accepted column modifiers accepted for Rails edge (> 4.2), that can be passed using curly braces:
limit Sets the maximum size of the string/text/binary/integer fields.
precision Defines the precision for the decimal fields, representing
the total number of digits in the number.
scale Defines the scale for the decimal fields, representing the number of digits after the decimal point.
polymorphic Adds a type column for belongs_to
associations.
null Allows or disallows NULL values in the column.
default Allows to set a default value on the column. Note that if you
are using a dynamic value (such as a date), the default will only be
calculated the first time (i.e. on the date the migration is applied).
index Adds an index for the column.
required Adds required: true for belongs_to associations and null: false to the column in the migration.
array: true is not a valid option for a field type in Rails. You can use serialize to store your array in a :text field and handle the default there.
So in your example:
bin/rails generate model Post player_name:text:required player_email:text:required tokens_collected_by_level:text items_collected:text