Skip to content

Instantly share code, notes, and snippets.

View adammcarth's full-sized avatar

Adam McArthur adammcarth

View GitHub Profile
@adammcarth
adammcarth / edit.html.erb
Last active December 10, 2015 00:38
NoMethodError - Ruby on Rails
<!-- Someone can visit http://localhost:3000/profiles/my-permalink and this page will display (this part is working fine!). -->
<h2>Edit your profile, <%= @user.first_name %>.</h2>
<%= form_for @user, :html => { :class => "form-horizontal"} do |f| %>
<%= f.label :first_name, "Your First Name" %>
<%= f.text_field :first_name %>
<%= f.submit "Update Profile" %>
<% end %>
class UserSession < Authlogic::Session::Base
def remember_me_for
3.months
end
end
@adammcarth
adammcarth / andrewpank.js
Last active December 10, 2015 14:59
:)
var andrewpank = array(
"watermelon",
"pinapple",
"strawberrires"
);
@adammcarth
adammcarth / aim.txt
Created March 10, 2013 07:57
If statement inside an image_tag
What I'm trying to achieve?
I want to have a left tooltip (tooltip-left) for the 2 results which display on the left hand side, and a right tooltip (tooltip-right) for the other 2 results which display on the right.
@adammcarth
adammcarth / search.rb
Last active December 21, 2015 11:59
SQL syntax to execute a search from query string.
# URL Visiting: example.com/posts?search=My+search+text&m=aug&y=13
search_sql = "MATCH (title,tags) AGAINST ('query_string('search')' IN BOOLEAN MODE)"
#=> MATCH (title,tags) AGAINST ('My search text' IN BOOLEAN MODE)
month_sql = "created_at LIKE ('%-#{return_month_number(query_string('m'))}-%')"
#=> created_at LIKE ('%-08-%')
year_sql = "MATCH (created_at) AGAINST ('#{(Time.now.strftime('%Y')[0..1]).to_s + query_string('y').to_s}' IN BOOLEAN MODE)"
#=> MATCH (created_at) AGAINST ('2013')
Post.where(
@adammcarth
adammcarth / comment.rb
Created October 9, 2013 10:36
Validating and saving a Gravatar URL
class AvatarValidator < ActiveModel::Validator
def validate(record)
unless gravatar_exists?
return record.errors[:avatar] << "No display picture could be found on Gravatar using #{record.avatar}."
end
end
end
class Comment < ActiveRecord::Base
has_one :post
@adammcarth
adammcarth / comment.rb
Last active December 25, 2015 03:59
Validate website url
class Comment < ActiveModel::Base
validate :valid_url?, :allow_blank => true
def valid_url?
if (self.website).start_with?("http://", "https://") and (self.website).end_with?(".*")
return true
elsif (self.website).end_with?(".*")
return true
self.website = "http://#{self.website}"
else
return self.errors[:website] << "The website you entered (#{self.website}) is invalid. Please try again."
@adammcarth
adammcarth / app.js
Last active December 25, 2015 16:09
Ajax post request in jQuery
var commentForm = $("form#newComment");
commentForm.submit(function() {
event.preventDefault();
$("#commentName").attr("disabled", true);
$("#commentGravatar").attr("disabled", true);
$("#commentWebsite").attr("disabled", true);
$("#commentBody").attr("disabled", true);
$("#createComment").attr("disabled", true);
@adammcarth
adammcarth / app.js
Last active December 25, 2015 18:39
Working with a json response in jQuery.
$.ajax({
// blah-blah-blah,
success: outputSomething
});
function outputSomething(data) {
$.each(data.errors, function(field, message) {
alert(field); // alert("body"), alert ("name")
})
}
@adammcarth
adammcarth / example.rb
Last active August 29, 2015 13:57
Using ("dynamic"?) classes in ruby...
# Active Record lets you do things like:
Post.find(3).title
#=> "Hello, world!"
# Cool. So my question is, how exactly do you setup a class that allows the
# programmer to use there own keyword? My use case:
def get(type, id)