Skip to content

Instantly share code, notes, and snippets.

@hidenowt
Created November 13, 2008 14:14
Show Gist options
  • Save hidenowt/24437 to your computer and use it in GitHub Desktop.
Save hidenowt/24437 to your computer and use it in GitHub Desktop.
# Controller do Documents
#########################
class DocumentsController < ApplicationController
before_filter :load_project
# GET /documents
# GET /documents.xml
def index
@documents = @project.documents.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @documents }
end
end
# GET /documents/1
# GET /documents/1.xml
def show
@document = @project.documents.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @document }
end
end
# GET /documents/new
# GET /documents/new.xml
def new
@document = @project.documents.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @document }
end
end
# GET /documents/1/edit
def edit
@document = @project.documents.find(params[:id])
end
# POST /documents
# POST /documents.xml
def create
@document = @project.documents.new(params[:document])
respond_to do |format|
if @document.save
flash[:notice] = 'Document was successfully created.'
format.html { redirect_to([@project, @document]) }
format.xml { render :xml => @document, :status => :created, :location => @document }
else
format.html { render :action => "new" }
format.xml { render :xml => @document.errors, :status => :unprocessable_entity }
end
end
end
# PUT /documents/1
# PUT /documents/1.xml
def update
@document = @project.documents.find(params[:id])
respond_to do |format|
if @document.update_attributes(params[:document])
flash[:notice] = 'Document was successfully updated.'
format.html { redirect_to([@project, @document]) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @document.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /documents/1
# DELETE /documents/1.xml
def destroy
@document = @project.documents.find(params[:id])
@document.destroy
respond_to do |format|
format.html { redirect_to(project_documents_url(@project)) }
format.xml { head :ok }
end
end
protected
def load_project
@project = Project.find(params[:project_id], :include => :documents)
end
end
# Modelo do Documents
#########################
class Document < ActiveRecord::Base
#relacionamentos
belongs_to :project
#validações
validates_presence_of :title
validates_presence_of :project_id
#attachment_fu
has_attachment :storage => :file_system, :max_size => 500.kilobytes, :path_prefix => 'public/uploads/documents'
validates_as_attachment
end
# Visoes do Documents
#########################
# _form
#######
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<% if @document.new_record? %>
<p>
<label for="uploaded_data">Upload:</label>
<%= f.file_field :uploaded_data %>
</p>
<% end %>
# edit
######
<h1>Editing document</h1>
<% form_for([@project, @document], :html => { :multipart => true }) do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', [@project, @document] %> |
<%= link_to 'Back', project_documents_path(@project) %>
# index
#######
<h1>Listing documents from project <%= @project.name %></h1>
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Filename</th>
<th>Size</th>
<th>Content type</th>
<th>Project</th>
</tr>
<% for document in @documents %>
<tr>
<td><%=h document.title %></td>
<td><%=h document.description %></td>
<td><%=h document.filename %></td>
<td><%=h document.size %></td>
<td><%=h document.content_type %></td>
<td><%=h document.project %></td>
<td><%= link_to 'Show', [@project, document] %></td>
<td><%= link_to 'Edit', edit_project_document_path(@project, document) %></td>
<td><%= link_to 'Destroy', [@project, document], :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New document', new_project_document_path(@project) %>
# new
#####
<h1>New document</h1>
<% form_for([@project, @document], :html => { :multipart => true }) do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', project_documents_path(@project) %>
# show
######
<p>
<b>Title:</b>
<%=h @document.title %>
</p>
<p>
<b>Description:</b>
<%=h @document.description %>
</p>
<p>
<b>Download:</b>
<%= link_to "Download this document", @document.public_filename %>
</p>
<p>
<b>Project:</b>
<%=h @document.project.name %>
</p>
<%= link_to 'Edit', edit_project_document_path(@project, @document) %> |
<%= link_to 'Back', project_documents_path(@project) %>
# bem tem esse objeto mais ele não subiu nenhum arquivo, queria deletar
# e criar um novo com o arquivo.
#############################################################################
Listing documents from project Contract Project
Title Description Filename Size Content type Project
aaaa aaaaa #<Project:0xb68997b4> Show Edit Destroy
New document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment