Skip to content

Instantly share code, notes, and snippets.

@rimian
Created December 1, 2010 09:37
Show Gist options
  • Save rimian/723238 to your computer and use it in GitHub Desktop.
Save rimian/723238 to your computer and use it in GitHub Desktop.
- form_for @sock, :html => { :multipart => true } do |f|
- if @sock.errors.any?
#error_explanation
%h
= pluralize(@sock.errors.count, "error")
prohibited this sock from being saved:
%ul
- @sock.errors.full_messages.each do |msg|
%li
= msg
%fieldset
%legend
Select sock location
%table#container
%tr
%td#col-left
%p
Click your sock's location on the Map to populate the latitude and longitude.
.field
= f.label :latitude
%br
= f.text_field :latitude
.field
= f.label :longitude
%br
= f.text_field :longitude
%td#col-right
:javascript
var is_form = true;
= javascript_include_tag 'map_form'
= render 'layouts/map_canvas'
%fieldset
%legend
Fill in some details
.field
= f.label :title
%br
= f.text_field :title
.field
= f.label :body
%br
= f.text_field :body
%fieldset
%legend
Attach the image
.field
= f.file_field :image
.actions
= f.submit
class Sock < ActiveRecord::Base
attr_accessible(:user_id, :title, :body, :image, :latitude, :longitude)
attr_accessor(:nearby)
# validates(:title, :presence => true)
# validates(:body, :presence => true)
# validates(:longitude, :presence => true)
# validates(:latitude, :presence => true)
#validates :user, :presence => true
has_attached_file :image, :styles => { :medium => "300x402>", :thumb => "200x300>" }
belongs_to :user
# check if any socks were found near this location by another user
def self.nearby(latitude, longitude)
radius = 100
Sock.where("latitude BETWEEN ? AND ?", latitude - radius, longitude + radius)
end
end
class SocksController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
# GET /socks
# GET /socks.xml
def index
@socks = Sock.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @socks }
end
end
# GET /socks/1
# GET /socks/1.xml
def show
@sock = Sock.find(params[:id])
@nearby = Sock.nearby(0,0)
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @sock }
end
end
# GET /socks/new
# GET /socks/new.xml
def new
@sock = Sock.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @sock }
end
end
# GET /socks/1/edit
def edit
@sock = Sock.find(params[:id])
end
# POST /socks
# POST /socks.xml
def create
@sock = Sock.new(params[:sock])
respond_to do |format|
if @sock.save
format.html { redirect_to(@sock, :notice => 'Sock was successfully uploaded.') }
format.xml { render :xml => @sock, :status => :created, :location => @sock }
else
format.html { render :action => "new" }
format.xml { render :xml => @sock.errors, :status => :unprocessable_entity }
end
end
end
# PUT /socks/1
# PUT /socks/1.xml
def update
@sock = Sock.find(params[:id])
respond_to do |format|
if @sock.update_attributes(params[:sock])
format.html { redirect_to(@sock, :notice => 'Sock was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @sock.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /socks/1
# DELETE /socks/1.xml
def destroy
@sock = Sock.find(params[:id])
@sock.destroy
respond_to do |format|
format.html { redirect_to(socks_url) }
format.xml { head :ok }
end
end
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_uniqueness_of :username
attr_accessible :username, :email, :password, :password_confirmation, :remember_me
has_many :socks
end
class UsersController < ApplicationController
def socks
respond_to do |format|
format.html # socks.html.erb
format.xml { render :xml => @socks }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment