Skip to content

Instantly share code, notes, and snippets.

@ncuesta
Created December 11, 2017 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncuesta/37bd20b3b42d57401b925db568064b16 to your computer and use it in GitHub Desktop.
Save ncuesta/37bd20b3b42d57401b925db568064b16 to your computer and use it in GitHub Desktop.
Ejemplo de un helper method en el controller, utilizado en el controller y en la vista
<%- ... -%>
<h1>
Detalles del docente <%= display_teacher(@teacher) -%>
</h1>
<%- ... -%>
class TeachersController < ApplicationController
before_action :set_teacher, only: :show
def index
@teachers = Teacher.order(:surname, :name)
end
def show; end
def new; end
def create
@teacher = Teacher.new(teacher_params)
if @teacher.save
redirect_to teachers_path, notice: "El docente #{display_teacher(@teacher)} ha sido creado correctamente."
else
render :new
end
end
private
def display_teacher(teacher)
"#{teacher.surname}, #{teacher.name}"
end
helper_method :display_teacher
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment