Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.06 KB | None | 0 0
  1. class PostsController < ApplicationController
  2.   def index
  3.     @posts = Post.all.limit(10).order(id: :desc)
  4.     @post = Post.new
  5.     @instagram = Instagram.user_recent_media("3900669", {:count => 18})
  6.     @cover = Cover.last.url
  7.  
  8.     if params.has_key?(:search)
  9.       @posts = Post.search(params[:search]).order("id DESC").limit(10)
  10.     else
  11.       @posts = Post.all.order('id DESC').limit(10)
  12.     end
  13.  
  14.     @current = request.original_url.split('/')[4].to_i    
  15. end
  16.  
  17.   def new
  18.     @post = Post.new
  19.   end
  20.  
  21.   def create
  22.     @post = Post.new(post_params)
  23.  
  24.     if @post.save
  25.       redirect_to "http://178.62.102.154/admin"
  26.     end
  27.   end
  28.  
  29.   before_filter :authenticate_user!, only: [:manage, :new, :edit, :destroy, :admin]
  30.   def manage
  31.       @posts = Post.all.limit(10).order(id: :desc)
  32.  
  33.       if params[:search]
  34.         @posts = Post.search(params[:search]).order("id DESC").limit(10)
  35.       else
  36.         @posts = Post.all.order('id DESC').limit(10)
  37.       end
  38.  
  39.       @count = Post.count
  40.       if @count % 10 > 0
  41.         @pages = (@count / 10) + 1
  42.       else
  43.         @pages = (@count / 10)
  44.       end
  45.  
  46.       @current = request.original_url.split('/')[5].to_i
  47.       @per_page = Post.limit(10).offset(@current*10-10).order(id: :desc)
  48.   end
  49.  
  50.   def destroy
  51.     @posts = Post.find(params[:id])
  52.  
  53.     if @posts.destroy
  54.       redirect_to "http://178.62.102.154:3000/admin/manage/1"
  55.     end
  56.   end
  57.  
  58.   def edit
  59.     @posts = Post.find(params[:id])
  60.   end
  61.  
  62.   def inner
  63.     @slug = request.original_url.split('/')[4]
  64.     @post = Post.select("content").where(slug: @slug).last.content
  65.     @instagram = Instagram.user_recent_media("3900669", {:count => 18})
  66.   end
  67.  
  68.   def admin
  69.  
  70.   end
  71.  
  72.  
  73.   def type
  74.     render partial: "type", locals: { count: 30 }
  75.   end
  76.  
  77.   def post_params
  78.     params[:post][:image] = [:image1, :image2, :image3].map {|k| params[k] }.join(" ")
  79.     title = params[:post][:title]
  80.  
  81.     params[:post][:slug] = title.gsub(" ", "-")
  82.     params.require(:post).permit(:title, :content, :category, :post_type, :image, :date, :slug)
  83.   end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement