Skip to content

Instantly share code, notes, and snippets.

@aidataguy
Last active September 28, 2015 16:52
Show Gist options
  • Save aidataguy/89c4ee72bd593af16ebe to your computer and use it in GitHub Desktop.
Save aidataguy/89c4ee72bd593af16ebe to your computer and use it in GitHub Desktop.
searchkick + elasticsearch issue
<li class="navs">
<%= form_tag posts_path, method: :get do%>
<%= text_field_tag :search, params[:query], placeholder: "Search Blog", name: "nil" , required: "", class: "input-field", id: "post_search", autocomplete: "off" do %>
<%= submit_tag "", class: "material-icons search-box" %>
<% end %>
<% if params[:search].present? %>
<%= link_to "X", posts_path %>
<% end %>
<% end %>
</li>
class Post < ActiveRecord::Base
searchkick autocomplete: ['title', 'description', 'tag_list']
extend FriendlyId
friendly_id :title, use: :slugged
def should_generate_new_friendly_id?
new_record?
end
validates_presence_of :title, :description, :content, :tag_list, message: "can't be blank"
acts_as_taggable
belongs_to :user
mount_uploader :preview, PreviewUploader
end
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def new
@post = current_user.posts.build
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def edit
@post = Post.friendly.find(params[:id])
end
def update
@post = Post.friendly.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path
end
def index
if params[:tag]
@posts = Post.tagged_with(params[:tag]).paginate(page: params[:page], per_page: 5)
else
@posts = Post.order('created_at DESC').paginate(page: params[:page], per_page: 2)
end
if params[:nil].present?
@posts = Post.search(params[:nil]).paginate(page: params[:page])
else
@posts = Post.paginate(page: params[:page])
end
end
def show
@post = Post.friendly.find(params[:id])
end
def autocomplete
render json: Post.search(params[:query], autocomplete: true, limit: 5).map(&:title)
end
private
def find_post
@post = Post.friendly.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :description, :content, :tag_list, :preview)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment