Cambiar la ruta link_to en función de la variable auxiliar
Tengo un método auxiliar que quiero usar para varios modelos para evitar tener que repetir el mismo código varias veces.
def link_to_follow(instance:instance,type:type)
link_to('Unfollow', unfollow_books_path(id: instance.id),
method: :post,
id: "unfollow_link_#{instance.id}",
remote: true)
end
¿Cómo lo hago para que si paso type:'magazine'
como una variable entonces unfollow_books_path
se convierta en unfollow_magazines_path
?
Mostrar la mejor respuesta
Publicado : 6 March, 2018 @ 20:55
¿Qué tal algo como:
def link_to_follow(instance:instance, type:type)
link_to(
'Unfollow',
send("unfollow_#{type.pluralize}_path", instance),
method: :post,
id: "unfollow_link_#{instance.id}",
remote: true
)
end
Supongo que también podrías hacer:
def link_to_follow(instance:instance)
link_to(
'Unfollow',
send("unfollow_#{instance.class.name.underscore.pluralize}_path", instance),
method: :post,
id: "unfollow_link_#{instance.id}",
remote: true
)
end
Publicado : 6 March, 2018 @ 20:59
¡Ay! Eso tiene sentido. ¡Gracias!
Publicado : 6 March, 2018 @ 21:09