ActionController :: UrlGenerationError with ActionMailer when trying to display a link

0

How about ?, Well I tell you, I've been working with Rails and ActiveAdmin for a while, I have not had any major problems, I'm not an expert or anything, but there is a lot of help on the Internet that can help solve many doubts.

In my application I am creating maintenance for industrial equipment, I simply select a task, assign a technician (admin_user) and select a date for the start of maintenance. I am implementing the sending of a notification that will reach the email account of the technician (admin_user) selected for maintenance with the details associated with the maintenance created by the ActiveAdmin form.

Here I leave the details of the coding.

app / models / maintenance.rb

class Maintenance < ApplicationRecord

  extend Enumerize
  enumerize :state, in: [:pendiente, :en_curso, :finalizado, :cancelado], default: :pendiente

  include AASM

  belongs_to :task
  belongs_to :admin_user

  aasm column: 'state' do
    state :pendiente, initial: true
    state :en_curso, :finalizado, :cancelado

    event :aceptar do
      transitions from: :pendiente, to: :en_curso
    end

    event :finalizar do
      transitions from: :en_curso, to: :finalizado
    end

    event :cancelar do
      transitions from: [:pendiente, :en_curso]
    end
  end

end

app / admin / admin_maintenances.rb

ActiveAdmin.register Maintenance, as: 'Mantenimiento' do

permit_params :task_id, :admin_user_id, :created_at, :state, :end

form do |f|
    f.inputs do
      if !f.object.new_record?
        f.li do
          f.label 'Tarea'
          f.span f.object.task
        end
      else
        f.input :task, label: 'Tarea'
    end

      f.input :admin_user, as: :select, collection: AdminUser.where(role: 'tecnico'), label: 'Técnico'
      f.input :created_at, as: :date_time_picker, label: 'Inicio mantenimiento', class: 'input-date-time', hint: 'Fecha y hora en la que se realizará el mantenimiento.'
      f.input :end, as: :date_time_picker, label: 'Fin mantenimiento', class: 'input-date-time', hint: 'Si el mantenimiento tiene una duración de varios días.'
      f.actions
    end
  end

  controller do

    def create
    @maintenance = Maintenance.new(permitted_params[:maintenance])
      super do |format|
        MaintenanceMailer.nuevo_mantenimiento(@maintenance).deliver
        redirect_to admin_mantenimiento_url(@mantenimiento) and return if resource.valid?
      end
    end
  end
end

app / mailers / maintenance_mailer.rb

class MaintenanceMailer < ActionMailer::Base
  default from: '[email protected]'

  def nuevo_mantenimiento(maintenance)
    @maintenance = maintenance
    mail(to: @maintenance.admin_user.email, subject: 'Nuevo mantenimiento')
  end
end

app / views / maintenance_mailer / new_maintenance.html.erb

<h2>Hola, ha sido creado un nuevo mantenimiento.</h2>

<h3>Técnico asignado: <%= link_to @maintenance.admin_user.nombre, admin_admin_user_url(@maintenance.admin_user.id) %></h3>

  <ul>
    <li>Equipo: <%= @maintenance.task.equipment.nombre %></li>
    <li>Modelo: <%= @maintenance.task.equipment.modelo %></li>
    <li>Ubicacion: <%= @maintenance.task.equipment.ubicacion.presence || "No especificado" %></li>
    <li>Inicio mantenimiento: <%= l(@maintenance.created_at, format: :long) %></li>
    <li>Estado inicial: [<%= @maintenance.state %>]</li>
  </ul>

  <p>Más detalles <%= link_to @maintenance.task.equipment.nombre, admin_mantenimiento_url(@maintenance) %> </p>

I am generating an email preview with ActionMailer Preview

So far, everything is fine, the preview generates the link of the assigned technician and the corresponding maintenance which shows the name of the equipment and gives the link type: localhost: 3000 / admin / maintenance / 70

The problem arises when I do this sending the maintenance form, it shows me the following error:

This happens only with the link corresponding to maintenance , if I remove the link, the email is sent and even gives me the link that shows the assigned technician without any inconvenience.

What am I missing?

I remain attentive to the answers.

    
asked by Mosiah Ricardo 23.05.2018 в 16:34
source

1 answer

1

The error is in _app / admin / admin_maintenances.rb _:

def create
  @maintenance = Maintenance.new(permitted_params[:maintenance])

  super do |format|
    MaintenanceMailer.nuevo_mantenimiento(@maintenance).deliver
    redirect_to admin_mantenimiento_url(@mantenimiento) and return if resource.valid?
  end
end

You are creating a new instance of Maintenance but you are not saving it, therefore there is no id in the object you send to MaintenanceMailer (resulting in id: nil ).

To correct it you can use @maintenance.save after creating the object or use create instead of new ; I would be inclined to use save since you can validate that the object was created correctly before continuing; for example:

def create
  @maintenance = Maintenance.new(permitted_params[:maintenance])

  if @maintenance.save
    MaintenanceMailer.nuevo_mantenimiento(@maintenance).deliver
    redirect_to admin_mantenimiento_url(@mantenimiento) and return if resource.valid?
  else
    # redirect a página donde se crea el objeto. 
  end
end

With this you are defining the action create outside of ActiveAdmin , so it is important to remove super to avoid duplicating the creation of the object.

    
answered by 23.05.2018 / 17:36
source