Skip to content

Instantly share code, notes, and snippets.

@bjhaid
bjhaid / gist:3692321
Created September 10, 2012 17:28
Trying to make an ajax request from a bare html to a rails 3 app
Controller
class MongobrokersController < ApplicationController
skip_before_filter :verify_authenticity_token
#dont forget to change 56800 to 3600
def air_hour
@air = Mongobroker.where(status: "up", node: "air", timestamp: (Time.now - 86400)..Time.now)
render :json => @air
end
def ema_hour
#This Script would ask for a person's first name, middle name, last name, and age separately, and would print, My first name is Ayodele, Adeniyi Abejide, I am 28 years old
puts "what is your first name?"
first_name = gets.chomp
puts "what is your middle name?"
middle_name = gets.chomp
puts "what is your last name?"
last_name = gets.chomp
Controller Method
def search
if params[:search_option] == "ink"
@ink = Ink.where("name LIKE ?", "%#{params[:name]}%")
elsif params[:search_option] == "printer"
@ink = Ink.where("brand_name LIKE ? AND serie_name LIKE ? AND printer_name LIKE ?", "%#{params[:brand]}%", "%#{params[:series]}%", "%#{params[:printer]}%")
end
@search = @ink
render :html => @search
@bjhaid
bjhaid / inks_controller.rb
Created August 3, 2012 13:34
Sample controller
class InksController < ApplicationController
before_filter :db_check
end
@bjhaid
bjhaid / application_controller.rb
Created August 3, 2012 13:31
Application Controller
#The assumption here is a one-to-one mapping between controllers and models
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def db_check
model = self.class.to_s.gsub("Controller", '').singularize
Uploader.csv_loader(model) if eval(model).all.empty?
end
end
@bjhaid
bjhaid / gist:3247682
Created August 3, 2012 13:24
Uploading CSV files into Rails 3 Database
Model
uploader.rb
#The csv I am using were created from some code manipulation hence not well formatted, I am stripping off new lines and merging fields in some cases
class Uploader
def self.csv_loader(model)
File.open("#{Rails.root}/data/#{model}.csv", 'r').each do |row|
new_row = row.split(",")
case
when model == 'Brand'
Ruby code:
require 'csv'
class Phonecsvtoarray
attr_accessor :arr_path
def csv_to_array
arr = []
CSV.foreach(@arr_path) do |row|
arr << row
@bjhaid
bjhaid / gist:1835904
Created February 15, 2012 13:58
User Model
User Model:
include Mongoid::Document
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
field :username
field :firstname
View
<%= form_for(:session, :url => sessions_path) do |f| %>
<%= f.text_field :password %>
<% end %>
Controller
class SessionsController < ApplicationController
def new
end
There are 10 CNUM
The CNUM value is 54973
#<SOAP::RPC::Driver:#<SOAP::RPC::Proxy:http://172.18.100.13/KakawaCRM/soap.php>>
Wire dump:
opening connection to 172.18.100.13...
opened
= Request
<?xml version="1.0" encoding="utf-8" ?>