Last active
September 28, 2015 16:52
-
-
Save aidataguy/89c4ee72bd593af16ebe to your computer and use it in GitHub Desktop.
searchkick + elasticsearch issue
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 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 |
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 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