Skip to content

Instantly share code, notes, and snippets.

@jheg
jheg / gist:df2e9ed6afb51c91af9f
Created July 1, 2015 21:26
caching in Rails 4 book Agile Web Development with Rails 4
# in development.rb
config.action_controller.perform_caching = true
# in app/models/product.rb
def self.latest
Product.order(:updated_at).last
end
# in app/views/store/index.html.erb
<% cache [ 'store', Product.latest ] do %>
test "product is not valid unless title is at least 10 characters long" do
product = Product.new(
title: "Book",
description: "yyy",
price: 1,
image_url: "fred.jpg"
)
assert product.invalid?
assert_equal ["Please ensure title is at least 10 characters long"], product.errors[:title]
end
def create
<<<<<<< HEAD
@bookmark = Bookmark.new(bookmark_params)
@bookmark.save
flash[:notice] = "Bookmark added!"
redirect_to root_path
=======
@bookmark = Bookmark.new(params[:create])
if @bookmark.save
# render text: "Thanks for sending a GET request with cURL! #{request.body.read}"
➜ ruby_bookmark_drag git:(jlh_master_url_fixer_method) git pull origin master
remote: Counting objects: 26, done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 26 (delta 15), reused 15 (delta 4), pack-reused 0
Unpacking objects: 100% (26/26), done.
From github.com:powelljmp/ruby_bookmark_drag
* branch master -> FETCH_HEAD
0673691..2cec1be master -> origin/master
Auto-merging app/views/shared/_form.html.erb
CONFLICT (content): Merge conflict in app/views/shared/_form.html.erb
➜ ruby_bookmark_drag git:(jlh_master_url_fixer_method) tail -f log/development.log
Started GET "/assets/z_custom.js?body=1" for 127.0.0.1 at 2015-03-22 21:01:02 +0000
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2015-03-22 21:01:02 +0000
Started GET "/assets/create.js?body=1" for 127.0.0.1 at 2015-03-22 21:01:02 +0000
@jheg
jheg / 0_reuse_code.js
Last active August 29, 2015 14:17
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
@jheg
jheg / gist:76aad705b13cb43fcbbe
Created February 18, 2015 15:16
hitting the db
# routes
post 'todos/:id/pinned', to: 'todos#pinned', as: :pinned_todo
post 'todos/:id/unpin', to: 'todos#unpin', as: :unpin_todo
# controller
def pinned
flash[:notice] = 'Todo pinned for today'
@todo.pinned = true
@todo.save
redirect_to root_path
@jheg
jheg / gist:3ee11ef78e91ffb7e2e9
Created February 17, 2015 15:19
losing a trailing '-' if present
def generate_slug
the_slug = to_slug(self.title)
post = Post.find_by(slug: the_slug)
count = 2
while post && post != self
the_slug = append_suffix(the_slug, count)
post = Post.find_by(slug: the_slug)
count += 1
end
self.slug = the_slug.downcase
def make_slug(post_title)
the_slug = post_title.gsub(/[\W_]/, '-').squeeze.chop.downcase
return the_slug
end
def ensure_slug_not_duplicate
the_slug = make_slug(self.title)
post = Post.find_by(slug: the_slug)
counter = 2
while post && post != self