r1 - 18 Aug 2007 - 18:34:38 - HecPeAreYou are here: TWiki >  Rails Web > RailsSC > OpenTicket2VSC
./script/generate scaffold_resource ticket project_id:integer user_id:integer assigned_to:integer title:string description:text closed:integer created_at:datetime updated_at:datetime
#En  002_create_t...
t.column :closed, :integer, :size => 1, :default => 0
rake db:migrate
#Comprobamos q ya funciona, pero no carga el layout:
rm app/views/layouts/tickets.rhtml   

#app/models/project.rb
has_many :tickets, :order => "id desc", :dependent => :destroy
 #order => "id desc" indica en orden descendente (el último/más grande) primero
 #:dependent => :destroy indica q cuando se borre un proyecto se borren tb los tickets

#app/models/tickets.rb
belongs_to :project

#Para poder trabajar con url REST y poder pasar los id del proyecto a los tickets (según el documento del SC nº1)
#Cambiamos
map.resources :tickets
map.resources :projects
#por
map.resources :projects do |project|
  project.resources :tickets
end

#En app/views/projects/index.rhtml cambiamos
<td><%=h project.name %></td>
#por
<td><%=link_to project.name, tickets_path(project) %></td>

#En tickets_controller.rb cambiamos
@tickets = Ticket.find(:all)
#por
    @project = Project.find(params[:project_id])
    @tickets = @project.tickets

#Pero como vamos a hacer lo mismo en cada método,
#en tickets_controller.rb creamos la función:
  protected
  def find_project
    @project = Project.find(params[:project_id])
  end
#añadimos el before_filter :find_project
#y borramos la linea
  @project = Project.find(params[:project_id])
#Como vemos que ... se repite
#Modificamos el método anterior a:
  protected
  def find_project_and_ticket
    @project = Project.find(params[:project_id])
    @ticket = Ticket.find(params[:id]) if params[:id]
  end
#Modificamos el filter y borramos las lineas que sobran

#En app/views/tickets/new.rhtml
<% form_for(:ticket, :url => tickets_path) do |f| %>
#por
<% form_for(:ticket, :url => tickets_path(@project)) do |f| %>

#Mod. tickets_controller.rb ...
#
#En tickets/new.rhtml borramos los campos Project, User y Assigned_to, Closed, Created_at, Updated_at
#...
#Daba error...en index.rhtml. Borro esas lineas y ya veremos luego
#Se sigue modificando el index.rhtml
#En index.rhtml se cambia
    <td><%=h ticket.created_at %></td>
por
  <td><%= distance_of_time_in_words_to_now ticket.created_at%><br /><span class="grey"><%=h ticket.created_at.to_s(:short) %></span></td>
  
#y añado en openticket.css (aunq no lo hace en el video, ya lo tendria):
span.grey {
    color: grey;
}
#Editamos tickets/show.rhtml
#Copio el css del código fuente de Vicent _cap2, pq tiene más cosas q el otro
cp ../openticket_cap_2/public/stylesheets/openticket.css public/stylesheets/openticket.css

-- HecPeAre - 18 Aug 2007

Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r1 | More topic actions
 
Powered by TWiki

This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback