Skip to content

Instantly share code, notes, and snippets.

@john-hamnavoe
john-hamnavoe / rails_7_select_populate_other_field_on_form.md
Created January 4, 2023 12:37
Basic approach to having default value on field populated by selection on select box

Introduction

For example have form with select box which has list of reasons and when user selects the reason we want to populate an amount field on the form. The reason table has the amount as one of its columns. In this example we have adjusments and adjustment_reasons when user creates a new adjustment they pick reason it populates the amount with default value from the adjustment reason.

Stimulus Controller

We will create reason_controller.js in app/javascript/controllers.

@john-hamnavoe
john-hamnavoe / rails_7_nested_attribute_form.md
Created December 14, 2022 16:27
Dymanic nested forms rails 7

Introduction

Model with accepted nested attributes stimulus controller to allow adding/deleting of rows to the nested input. e.g. TimeLog might have many TimeLogEntries

Model Changes

Change to accepts nested attributes

  has_many :time_log_entries
  accepts_nested_attributes_for :time_log_entries, allow_destroy: true
@john-hamnavoe
john-hamnavoe / rails_7_stimulus_turbo_modal_dialog.md
Last active December 12, 2022 15:51
Using Modals in Rails 7 Application using Stimulus and Hotwire

Introduction

Putting modal dialog framework into Rails 7 application. Using this approach by David Colby. But not going as far as it does with dispatch events. As the article notes need to be on Turbo 7.2 at least.

Stimulus Controller

We will create modal_controller.js in app/javascript/controllers. Very similar to excid3 tailwind stimulus components modal component.

// This controller is an edited-to-the-essentials version of the modal component created by @excid3 as part of the essential tailwind-stimulus-components package found here:
@john-hamnavoe
john-hamnavoe / rails_7_using_sidekiq.md
Last active January 5, 2023 16:29
Basic Steps to Add Sidekiq to Rails 7 App

Gemfile

gem "sidekiq", "~> 6.2"
gem "sidekiq-failures"

Update config/application.rb

 class Application < Rails::Application
@john-hamnavoe
john-hamnavoe / rails_7_models.md
Created December 9, 2022 11:13
Random notes on model generation rails

Introduction

Some notes on syntax on models always looking up.

Foreign Keys

Optional

  • Change Migration to null: true e.g. t.references :vehicle, null: true, foreign_key: true
  • add optional: true to model e.g. belongs_to :vehicle, optional: true
@john-hamnavoe
john-hamnavoe / rails_7_index_container_view_component.md
Last active December 9, 2022 09:49
My Index Container For Used on Standard Index Page

Index Container Component

View component I use for Index pages that are linked up to pagy and ransack, this component provides standard interface with title, subtitle, search, active filter and new button. Styling can change as required.

Generate Component

rails g component IndexContainer

IndexContainerComponent.rb

@john-hamnavoe
john-hamnavoe / rails_7_table_view_component.md
Last active December 6, 2022 12:10
My approach to table view components

Table View Component

These components used to generate a table in Rails 7 app.

Generate components

rails g component Tables::Table
rails g component Tables::Row
rails g component Tables::Header
rails g component Tables::Cell

@john-hamnavoe
john-hamnavoe / rails_7_pagy_view_component.md
Last active December 6, 2022 12:55
My approach to pagy paginator using view component in Rails 7

View Component for Pagy Paginator

rails g compoent pagy_paginator

pagy_paginator_component.rb

# frozen_string_literal: true

class PagyPaginatorComponent < ApplicationComponent
@john-hamnavoe
john-hamnavoe / rails_7_pagy_ransack.md
Last active July 16, 2024 15:42
How I set up pagy and ransack for simple rails 7 application

Introduction

Adding pagy and ransack in rails so can have search function, paging and sorting on table of date on index controller. We also will use kredis to make it easy to store users last search/sort etc. between pages.

Install

Gemfile:

gem "pagy" gem "ransack"