Add documentation to customize controllers
This commit is contained in:
@@ -34,6 +34,7 @@
|
|||||||
* [Views and Styles](customization/views_and_styles.md)
|
* [Views and Styles](customization/views_and_styles.md)
|
||||||
* [Javascript](customization/javascript.md)
|
* [Javascript](customization/javascript.md)
|
||||||
* [Models](customization/models.md)
|
* [Models](customization/models.md)
|
||||||
|
* [Controllers](customization/controllers.md)
|
||||||
* [Components](customization/components.md)
|
* [Components](customization/components.md)
|
||||||
* [Gems](customization/gems.md)
|
* [Gems](customization/gems.md)
|
||||||
* [Overwriting Application](customization/overwriting.md)
|
* [Overwriting Application](customization/overwriting.md)
|
||||||
|
|||||||
26
docs/en/customization/controllers.md
Normal file
26
docs/en/customization/controllers.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# Customizing controllers
|
||||||
|
|
||||||
|
Just like models, controllers are written using Ruby code, so their customization is similar, only we'll use the `app/controllers/custom/` folder instead of the `app/models/custom/` folder. Check the [models customization](models.md) section for more information.
|
||||||
|
|
||||||
|
## Customizing allowed parameters
|
||||||
|
|
||||||
|
When customizing Consul Democracy, sometimes you might want to add a new field to a form. Other than [customizing the view](views.md) or [the component](components.md) that renders that form, you need to modify the controller so the new field is accepted. If not, the new field will silently be ignored; this is done to prevent [mass assignment attacks](https://en.wikipedia.org/wiki/Mass_assignment_vulnerability).
|
||||||
|
|
||||||
|
For example, let's say you've modified the `SiteCustomization::Page` model so it uses a field called `author_nickname` and you've added that field to the form to create a custom page in the admin area. To add the allowed parameter to the controller, create a file `app/controllers/custom/admin/site_customization/pages_controller.rb` with the following content:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
load Rails.root.join("app", "controllers", "admin", "site_customization", "pages_controller.rb")
|
||||||
|
|
||||||
|
class Admin::SiteCustomization::PagesController
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
alias_method :consul_allowed_params, :allowed_params
|
||||||
|
|
||||||
|
def allowed_params
|
||||||
|
consul_allowed_params + [:author_nickname]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Note we're aliasing and then calling the original `allowed_params` method, so all the parameters allowed in the original code will also be allowed in our custom method.
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
* [Views and Styles](views_and_styles.md)
|
* [Views and Styles](views_and_styles.md)
|
||||||
* [Javascript](javascript.md)
|
* [Javascript](javascript.md)
|
||||||
* [Models](models.md)
|
* [Models](models.md)
|
||||||
|
* [Controllers](controllers.md)
|
||||||
* [Components](components.md)
|
* [Components](components.md)
|
||||||
* [Gems](gems.md)
|
* [Gems](gems.md)
|
||||||
* [Overwriting Application](overwriting.md)
|
* [Overwriting Application](overwriting.md)
|
||||||
|
|||||||
@@ -34,6 +34,7 @@
|
|||||||
* [Vistas y Estilos](customization/views_and_styles.md)
|
* [Vistas y Estilos](customization/views_and_styles.md)
|
||||||
* [Javascript](customization/javascript.md)
|
* [Javascript](customization/javascript.md)
|
||||||
* [Modelos](customization/models.md)
|
* [Modelos](customization/models.md)
|
||||||
|
* [Controladores](customization/controllers.md)
|
||||||
* [Componentes](customization/components.md)
|
* [Componentes](customization/components.md)
|
||||||
* [Gemas](customization/gems.md)
|
* [Gemas](customization/gems.md)
|
||||||
* [Adaptar la aplicación](customization/overwriting.md)
|
* [Adaptar la aplicación](customization/overwriting.md)
|
||||||
|
|||||||
26
docs/es/customization/controllers.md
Normal file
26
docs/es/customization/controllers.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# Personalización de controladores
|
||||||
|
|
||||||
|
Al igual que los modelos, los controladores están escritos en Ruby, con lo cual su personalización es similar solo que usaremos el directorio `app/controllers/custom/` en lugar del directorio `app/models/custom/`. Echa un vistazo a la documentación de [personalización de modelos](models.md) para más información.
|
||||||
|
|
||||||
|
## Personalización de parámetros permitidos
|
||||||
|
|
||||||
|
Al personalizar Consul Democracy, a veces querrás añadir un nuevo campo en un formulario. Además de [personalizar la vista](views.md) o [el componente](components.md) que renderiza ese formulario, tendrás que modificar el controlador para que acepte el nuevo campo. En caso contrario, el nuevo campo será ignorado completamente; esta práctica se utiliza para evitar [ataques de asignación masiva](https://en.wikipedia.org/wiki/Mass_assignment_vulnerability).
|
||||||
|
|
||||||
|
Por ejemplo, supongamos que has modificado el modelo `SiteCustomization::Page` para que utilice un campo llamado `author_nickname` y has añadido ese campo al formulario para crear una nueva página en el área de administración. Para añadir este campo a la lista de parámetros permitidos por el controlador, crea el archivo `app/controllers/custom/admin/site_customization/pages_controller.rb` con el siguiente contenido:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
load Rails.root.join("app", "controllers", "admin", "site_customization", "pages_controller.rb")
|
||||||
|
|
||||||
|
class Admin::SiteCustomization::PagesController
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
alias_method :consul_allowed_params, :allowed_params
|
||||||
|
|
||||||
|
def allowed_params
|
||||||
|
consul_allowed_params + [:author_nickname]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Nótese cómo estamos creando un alias para el método original `allowed_params` y luego estamos llamando a ese alias. De esta manera, todos los parámetros permitidos por el código original también estarán permitidos en nuestro método personalizado.
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
* [Vistas y Estilos](views_and_styles.md)
|
* [Vistas y Estilos](views_and_styles.md)
|
||||||
* [Javascript](javascript.md)
|
* [Javascript](javascript.md)
|
||||||
* [Modelos](models.md)
|
* [Modelos](models.md)
|
||||||
|
* [Controladores](controllers.md)
|
||||||
* [Componentes](components.md)
|
* [Componentes](components.md)
|
||||||
* [Gemas](gems.md)
|
* [Gemas](gems.md)
|
||||||
* [Adaptar la aplicación](overwriting.md)
|
* [Adaptar la aplicación](overwriting.md)
|
||||||
|
|||||||
Reference in New Issue
Block a user