User Tools

Site Tools


ruby

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
ruby [2026/03/14 08:58] – created reddyruby [2026/03/15 19:56] (current) reddy
Line 6: Line 6:
  
   * [[Kaminari]]   * [[Kaminari]]
 +  * [[Ransack]]
 +  * [[Railties]]
 +
 +==== Seed Data ====
 +
 +<code>
 +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
 +</code>
 +
 +==== Scaffolding ====
 +
 +<code>
 +rails g scaffold MyModel field1:string
 +</code>
 +
 +Data Types:
 +  * binary
 +  * boolean
 +  * date
 +  * datetime
 +  * decimal
 +  * float
 +  * integer
 +  * primary_key
 +  * string
 +  * text
 +  * time
 +  * timestamp
 +  * references
 +
 +Example for references:
 +
 +<code>
 +rails g scaffold Country name:string
 +rails g scaffold City name:string country:references
 +</code>
 +
 +==== Multiple References ====
 +
 +To create multiple references to another model (for different attribute fields):
 +
 +<code>
 +# 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'
 +</code>
 +
 +==== Resetting the Database ====
 +
 +<code>
 +rm storage/development.sqlite db/schema.rb
 +rails db:drop db:create db:migrate
 +</code>
 +
 +==== 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.
 +
 +<code>
 +# 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 %>
 +
 +</code>
 +
 +==== Turbo Streams ====
 +
 +<code>
 +# In the view
 +<%= turbo_stream_from "src_1" %>
 +<div id="divvy_1"></div>
 +
 +# In a controller
 +Turbo::StreamsChannel.broadcast_update_to(
 +  'src_1',
 +  target: 'divvy_1',
 +  content: DateTime.now  # html: render ...
 +)
 +</code>
 +
ruby.1773475112.txt.gz · Last modified: by reddy

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki