User Tools

Site Tools


ruby

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ruby [2026/03/14 09:28] reddyruby [2026/03/15 19:56] (current) reddy
Line 7: Line 7:
   * [[Kaminari]]   * [[Kaminari]]
   * [[Ransack]]   * [[Ransack]]
 +  * [[Railties]]
  
 ==== Seed Data ==== ==== Seed Data ====
Line 48: Line 49:
 rails g scaffold City name:string country:references rails g scaffold City name:string country:references
 </code> </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.1773476894.txt.gz · Last modified: by reddy

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki