Skip to content

Instantly share code, notes, and snippets.

@delba
delba / new.html.erb
Last active December 15, 2015 21:19
Login with email or username
<%= form_tag :login do %>
<%= label_tag :login, "Email or Username" %>
<%= text_field_tag :login %>
<%= submit_tag "Log In" %>
<% end %>
@delba
delba / application_controller.rb
Last active December 17, 2015 00:29
Redirect to action after authentication
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
private
def signed_in?
!!session[:user_id]
end
helper_method :signed_in?
@delba
delba / application.html.erb
Last active December 17, 2015 00:29
Title helper
<!DOCTYPE html>
<html>
<head>
<title><%= content_for?(:title) ? yield(:title) : "ApplicationName" %></title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
@delba
delba / new.html.erb
Last active December 17, 2015 00:29
Show password checkbox
<%= form_for @user, url: :signup do |f| %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password, autocomplete: 'off' %>
<input type="checkbox" id="show_password" />
<label for="show_password">Show Password</label>
@delba
delba / items.js.coffee
Last active December 17, 2015 00:29
Keyboard navigation on list
container = document.getElementById('items')
new List(container)
@delba
delba / confirm.html.erb
Last active December 17, 2015 00:29
Confirm delete
<%= form_tag @item, method: :delete do |f| %>
<%= label_tag :name, "Type the item's name to delete" %>
<%= text_field_tag :name, nil,
data: { name: @item.name }, autocomplete: 'off' %>
<%= submit_tag "Delete Item", disabled: true %>
<% end %>
@delba
delba / item.rb
Last active December 17, 2015 01:29
Starring items with redis
class Item < ActiveRecord::Base
def stars
User.find star_ids
end
def stars_count
$redis.scard redis_key(:stars)
end
private
@delba
delba / gravatar.rb
Last active December 17, 2015 03:09
Gravatar
module Gravatar
BASE_URL = "http://www.gravatar.com/avatar".freeze
def gravatar_url(options = {})
"#{BASE_URL}/#{email_hash}?#{options.to_query}"
end
private
def email_hash
@delba
delba / application_controller.rb
Last active December 17, 2015 03:09
Nesting a resource under its owner's username
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
private
# use default_url_options ?
def item_path(item)
super(item.user, item)
end
helper_method :item_path
end
@delba
delba / items_controller.rb
Created May 9, 2013 17:34
Using multiple submit buttons
class ItemsController < ApplicationController
def create
@item = Item.create!(item_params)
end
private
def item_params
params.require(:item).permit(:name, :public)
end