Server side search
Michael Pope
Server Setup
- Rails 3.2.x
- Ransack
Client Setup
- AngularJS 1.0.7
- Angular-resource
- Angular-ui-router
- Jasmine + Karma
Angular Rails Best Practices
- Coffeescript
- Testing with Jasmine & Karma
- HTML5 pushstate
- Flash messages with timeout
- ui.router with nested views
- Searching
AngularJS Filters
Only good for small data sets
AngularJS ng-table
Ransack
Search form
<form>
<tr>
<td><input ng-model="subject_cont" /></td>
<td><input ng-model="body_cont" /></td>
<td>
<input type="reset" value="reset" ng-click="reset()" />
<input type="submit" name="submit" value="submit" ng-click="search()" />
</td>
</tr>
</form>
AngualrJS search method
$scope.search = ->
searchService.setSubject($scope.subject_cont)
searchService.setBody($scope.body_cont)
# Submit the search
$http.get('/api/letters?' +
'q[subject_cont]=' + $scope.subject_cont +
'&q[body_cont]=' + $scope.body_cont)
.then ((response) ->
$scope.letters=response.data
)
Rails controller
class Api::LettersController < Api::BaseController
def index
@q=Letter.search(params[:q])
@letters=@q.result()
respond_with :api, @letters
end
end
Remember values
# Retain search variables whilst moving through the application
app.service('searchService', ->
subject = ''
body = ''
getSubject : -> return subject
setSubject : (text) -> subject = text
getBody : -> return body
setBody : (text) -> body = text
)
Load values
if $state.current.name == 'letters'
$scope.subject_cont = searchService.getSubject()
$scope.body_cont = searchService.getBody()
$scope.search()