Created
January 29, 2016 17:17
-
-
Save ollac21/0ab136d46d06e37fb2c2 to your computer and use it in GitHub Desktop.
App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.Diseases = function() { | |
this._input = $('#diseases-search-txt'); | |
this._initAutocomplete(); | |
}; | |
app.Diseases.prototype = { | |
_initAutocomplete: function() { | |
this._input | |
.autocomplete({ | |
source: '/diseases', | |
appendTo: '#diseases-search-results', | |
select: $.proxy(this._select, this) | |
}) | |
.autocomplete('instance')._renderItem = $.proxy(this._render, this); | |
}, | |
_render: function(ul, item) { | |
var markup = [ | |
'<span class="name">' + item.name + '</span>' | |
]; | |
return $('<li>') | |
.append(markup.join('')) | |
.appendTo(ul); | |
}, | |
_select: function(e, ui) { | |
this._input.val(ui.item.name); | |
return false; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Disease < ActiveRecord::Base | |
def self.search(term) | |
where('LOWER(name) LIKE :term', term: "%#{term.downcase}%") | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DiseasesController < ApplicationController | |
before_action :set_disease, only: [:show, :edit, :update, :destroy] | |
def index | |
respond_to do |format| | |
format.html | |
format.json { @diseases = Disease.search(params[:term]) } | |
end | |
end | |
def show | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_disease | |
@disease = Disease.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def disease_params | |
params.require(:disease).permit(:name) | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1> Type any disease to find info about it </h1> | |
<div class="diseases-search"> | |
<input type="text" id="diseases-search-txt" autofocus> | |
<div class="results" id="diseases-search-results"></div> | |
</div> | |
<script> | |
$(function() { | |
new app.Diseases; | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json.array!(@diseases) do |disease| | |
json.name disease.name | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
resources :diseases do | |
get :search, :on => :collection | |
end | |
resources :handlers, :only => [:index] | |
root 'diseases#index' | |
#match 'diseases/set_disease/:id' => 'diseases#show', as: :diseases_show | |
# The priority is based upon order of creation: first created -> highest priority. | |
# See how all your routes lay out with "rake routes". | |
# You can have the root of your site routed with "root" | |
# root 'welcome#index' | |
# Example of regular route: | |
# get 'products/:id' => 'catalog#view' | |
# Example of named route that can be invoked with purchase_url(id: product.id) | |
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase | |
# Example resource route (maps HTTP verbs to controller actions automatically): | |
# resources :products | |
# Example resource route with options: | |
# resources :products do | |
# member do | |
# get 'short' | |
# post 'toggle' | |
# end | |
# | |
# collection do | |
# get 'sold' | |
# end | |
# end | |
# Example resource route with sub-resources: | |
# resources :products do | |
# resources :comments, :sales | |
# resource :seller | |
# end | |
# Example resource route with more complex sub-resources: | |
# resources :products do | |
# resources :comments | |
# resources :sales do | |
# get 'recent', on: :collection | |
# end | |
# end | |
# Example resource route with concerns: | |
# concern :toggleable do | |
# post 'toggle' | |
# end | |
# resources :posts, concerns: :toggleable | |
# resources :photos, concerns: :toggleable | |
# Example resource route within a namespace: | |
# namespace :admin do | |
# # Directs /admin/products/* to Admin::ProductsController | |
# # (app/controllers/admin/products_controller.rb) | |
# resources :products | |
# end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p> | |
<strong>Name:</strong> | |
<%= @disease.name %> | |
</p> | |
<%= link_to 'Back', diseases_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json.extract! @disease, :id, :name, :created_at, :updated_at |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment