Skip to content

Instantly share code, notes, and snippets.

@elricstorm
Created June 29, 2014 13:38
Show Gist options
  • Save elricstorm/006c074cc9d94bb77f89 to your computer and use it in GitHub Desktop.
Save elricstorm/006c074cc9d94bb77f89 to your computer and use it in GitHub Desktop.
class AjaxController < ApplicationController
skip_authorization_check
def ncaa_teams
if params[:search]
like= "%".concat(params[:search].concat("%"))
teams = NcaaTeam.where("name like ?", like)
else
teams = NcaaTeam.all
end
list = teams.map {|u| Hash[ id: u.id, label: u.name, name: u.name]}
render json: list
end
def nfl_teams
if params[:search]
like= "%".concat(params[:search].concat("%"))
teams = NflTeam.where("name like ?", like)
else
teams = NflTeam.all
end
list = teams.map {|u| Hash[ id: u.id, label: u.name, name: u.name]}
render json: list
end
end
*= require myfoundation/foundation
*= require site
*= require responsive-tables
*= require jquery.ui.autocomplete
*= require administration
*= require users
*= require subscriptions
*= require ncaa
*= require nfl
*/
//= require jquery
//= require jquery.ui.autocomplete
//= require jquery_ujs
//= require responsive-tables
//= require foundation
//= require jquery.purr
//= require best_in_place
//= require sticky_footer
$(document).ready ->
$('#ncaa_team_name').autocomplete({source: "/ajax/ncaa_teams"});
$('#nfl_team_name').autocomplete({source: "/ajax/nfl_teams"});
$(function(){ $(document).foundation(); });
# model for NcaaTeam
def ncaa_team_name=(name)
team = NcaaTeam.find_by_name(name)
if team
self.team_id = team.id
else
errors[:team_name] << "Invalid name entered"
end
end
def ncaa_team_name
NcaaTeam.find(team_id).name if team_id
end
# model for NflTeam
def nfl_team_name=(name)
team = NflTeam.find_by_name(name)
if team
self.team_id = team.id
else
errors[:team_name] << "Invalid name entered"
end
end
def nfl_team_name
NflTeam.find(team_id).name if team_id
end
<li class="has-form">
<div class="row collapse">
<div class="small-12 columns">
<div class="centered">
<%= form_tag nfl_teams_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search], id: "nfl_team_name", placeholder: 'Find a Team' %>
<%= submit_tag "Search", :name => nil, class: 'round button tiny' %>
</p>
<% end %>
</div>
</div>
</div>
</li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment