Skip to content

Instantly share code, notes, and snippets.

<%= form_for @site, url: site_pass_path(@site.name), method: :patch do |f| %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit "Add password" %>
<% end %>
class Site < ApplicationRecord
before_save { self.name = name.downcase }
before_create :lock_init
VALID_NAME_REGEX = /\A[a-zA-Z\d\-]+\z/i # letters, numbers, dashes
validates :name, presence: true, uniqueness: true, length: { maximum: 50 }, format: { with: VALID_NAME_REGEX }
has_secure_password validations: false
private
class SitesController < ApplicationController
before_action :find_site, only: [:show, :add_password, :remove_password]
before_action :unlocked?, only: [:add_password, :remove_password]
before_action :have_posts?, only: [:add_password]
def show
...
end
@eri-b
eri-b / site.rb
Last active September 14, 2019 16:46
class Site < ApplicationRecord
before_create :lock_init
VALID_NAME_REGEX = /\A[a-zA-Z\d\-]+\z/i # letters, numbers, dashes
validates :name, presence: true, uniqueness: true, length: { maximum: 50 }, format: { with: VALID_NAME_REGEX }
has_secure_password validations: false
private
def lock_init
self.locked = false
class SitesController < ApplicationController
before_action :find_site, only: [:show]
def show
if @site.nil?
@site = Site.new(name: @slug)
if @site.save
flash.now[:notice] = "Your site is ready. Hi #{@slug} :)"
else