====== Ruby ======
===== Rails =====
Popular Gems
* [[Kaminari]]
* [[Ransack]]
* [[Railties]]
==== Seed Data ====
bundle add faker # Add it to only the development and test groups, if you are a stickler for convention
# In db/seeds.rb
50.times do
MyModel.create! name: Faker::Name.name
end
rails db:migrate && rails db:seed
==== Scaffolding ====
rails g scaffold MyModel field1:string
Data Types:
* binary
* boolean
* date
* datetime
* decimal
* float
* integer
* primary_key
* string
* text
* time
* timestamp
* references
Example for references:
rails g scaffold Country name:string
rails g scaffold City name:string country:references
==== Multiple References ====
To create multiple references to another model (for different attribute fields):
# The other model
rails g Country name
# In the scaffold command
rails g scaffold Staff residing_country:references
# In the migration
t.references :residing_country, null: false, foreign_key: { to_table: :countries }
# In the model
belongs_to :residing_country, class_name: 'Country', foreign_key: 'residing_country_id'
==== Resetting the Database ====
rm storage/development.sqlite db/schema.rb
rails db:drop db:create db:migrate
==== Turbo Frames ====
Turbo frames are for partial view updates. When the view is being rendered, return content within the turbo frame tag with the same identifier. domid(model_object) can be used to generate an identifier.
# In home/index view
<%= turbo_frame_tag "str_1" do %>
This is a thing
<%= link_to 'Action', controller: 'home', action: 'info'
<% end %>
# In home/info view
<%= turbo_frame_tag "str_1" do %>
This is another thing
<%= link_to 'Action', controller: 'home', action: 'index'
<% end %>
==== Turbo Streams ====
# In the view
<%= turbo_stream_from "src_1" %>
# In a controller
Turbo::StreamsChannel.broadcast_update_to(
'src_1',
target: 'divvy_1',
content: DateTime.now # html: render ...
)