Managing the behavior of the "Cancel" link in Rails' ActiveAdmin forms

257 Views Asked by At

A typical form produced by the ActiveAdmin DSL might look like so:

ActiveAdmin.register Post do
  ...
  form do |f|
    f.inputs do
      f.input :title
      f.input :content
    end
    f.actions
  end
  ...
end

The f.actions bit renders both a submit button and a cancel link. The problem is that the latter's target is always the index page. Instead, for a previously persisted record I would like to be taken back to the show page. As a possible workaround, f.actions could be replaced with:

f.actions do
  f.action :submit
  if resource.persisted?
    f.cancel_link({action: :show})
  else
    f.cancel_link
  end
end

This, however, seems verbose and clunky. Am I missing some more idiomatic trick to accomplish the same?

0

There are 0 best solutions below