From f51aed84d635c018df5baa5bde598d5c2b8ceaee Mon Sep 17 00:00:00 2001 From: f Date: Mon, 14 May 2018 18:45:52 -0300 Subject: [PATCH] procesar los campos de la plantilla como strong params --- app/controllers/posts_controller.rb | 14 ++++++-------- app/models/post.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 7787bab..7fe23a8 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -49,12 +49,11 @@ class PostsController < ApplicationController end def update - p = post_params @site = find_site @lang = find_lang(@site) @post = find_post(@site) - @post.update_attributes(p) + @post.update_attributes(post_params) if @post.save redirect_to site_posts_path(@site, category: session[:category], lang: @lang) @@ -67,11 +66,10 @@ class PostsController < ApplicationController # 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: []) + default_post_params = [:title, :date, :content, :slug, :cover, + :layout, :permalink, :dir, + { lang: {} }, { tags: [] }, { categories: [] }] + + params.require(:post).permit(default_post_params + @post.template_params) end end diff --git a/app/models/post.rb b/app/models/post.rb index 8d2d8bb..ff8f134 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -227,6 +227,18 @@ class Post end.keys end + # devuelve las plantillas como strong params + def template_params + @template_params ||= template_fields.map do |k| + v = template.get_front_matter(k) + if v.is_a? Array + { k.to_sym => [] } + else + k.to_sym + end + end + end + # Obtiene el tipo de campo para la plantilla def template_form_type_for(field) return if (tt = template.get_front_matter(field)).nil?