class PostsController < ApplicationController before_action :authenticate! def index @site = find_site @lang = find_lang(@site) @category = session[:category] = params.dig(:category) @posts = @site.posts_for(@lang) if params[:sort_by].present? @posts.sort_by! do |p| p.send(params[:sort_by].to_s) end end end def show @site = find_site @lang = find_lang(@site) @post = find_post(@site) end def new @site = find_site @lang = find_lang(@site) @post = Post.new(site: @site, front_matter: { date: Time.now }, lang: @lang) end def create @site = find_site @lang = find_lang(@site) @post = Post.new(site: @site, front_matter: post_params.to_hash, lang: @lang) if @post.save redirect_to site_posts_path(@site, lang: @lang) else render 'posts/new' end end def edit @site = find_site @lang = find_lang(@site) @post = find_post(@site) end def update p = post_params @site = find_site @lang = find_lang(@site) @post = find_post(@site) @post.update_attributes(p) if @post.save redirect_to site_posts_path(@site, category: session[:category], lang: @lang) else render 'posts/edit' end end private # Solo permitir cambiar estos atributos de cada articulo def post_params params.require(:post).permit(:title, :date, :content, :slug, :cover, :layout, :permalink, :objetivos, :dir, :duracion, :formato, :habilidades, :recomendaciones, conocimientos: [], sesiones_ejercicios_relacionados: [], materiales_requeridos: [], lang: {}, tags: [], categories: []) end end