Skip to content

Instantly share code, notes, and snippets.

@Mizpah
Created February 25, 2013 00:12
Show Gist options
  • Save Mizpah/5026381 to your computer and use it in GitHub Desktop.
Save Mizpah/5026381 to your computer and use it in GitHub Desktop.
The list game_classes page used to work, with sortable tables. It used a partial for the table body (header was in the index). Added pagination, better sortable tables, and a search field via several railscasts. I started to add ajaxification to all of the elements. Each intended change worked, so I am unsure where I introduced the error. Now th…
<table class="pretty">
<thead>
<tr>
<th><%= sortable "name" %></th>
<th>Description</th>
<th><%= sortable "game_id" %></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="game_classes">
<tr>
<td><%= game_class.name %></td>
<td><%= game_class.description %></td>
<td><%= game_class.game_id %></td>
<td><%= link_to 'Show', game_class %></td>
<td><%= link_to 'Edit', edit_game_class_path(game_class) %></td>
<td><%= link_to 'Destroy', game_class, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</tbody>
</table>
<div id="paginator">
<%= paginate @game_classes %>
</div>
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end
end
class GameClass < ActiveRecord::Base
attr_accessible :description, :name, :game_id
belongs_to :game
validates_presence_of :name
validates_presence_of :description
validates_presence_of :game_id
validates :name, :uniqueness => { :scope => :game_id, :message => "of a class may not be duplicated within a game!!" }
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
end
class GameClassesController < ApplicationController
# GET /game_classes
# GET /game_classes.json
# added to allow controller to generate parameters if not present. railscast 228
helper_method :sort_column, :sort_direction
def index
@game_classes = GameClass.search(params[:search]).order(sort_column + ' ' + sort_direction).page(params[:page]).per(10)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @game_classes }
end
end
# Methods to set sort peramiters securely (checking whats in the query string)
private
def sort_column
GameClass.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
<h1>Listing game_classes</h1>
<%= form_tag game_classes_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
</p>
<%- end %>
foo
<div id="game_classes"><%= render @game_classes %></div>
bah
<p><%= link_to "New Class", new_game_class_path %></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment