ruby-on-railsactiveadminarbre

How to write conditional ruby code to show in form of .arb file (Active Admin)


I want to fetch the Attachment Model's attribute name(pdf_file.file_name) based on condition (if..else). But I am not familiar with Arbe Syntex and cant show the outputs in view.

I am rendering a partial file from my active_admin class.

The code in my _test.arb file

f.has_many :course_pdf_attachments, allow_destroy: true do |attachment|
  if attachment.object.pdf_file.present?
   'Uploaded PDF:'
   link_to attachment.object.pdf_file.filename.upcase,
           attachment.object.pdf_file_url
  end
  attachment.input :pdf_file, as: :file
end

The output I am getting in the view is only the input_filed not the value that I'm trying to show in the conditions.

How to write this code so that my values are visible in the view?

Thanks in Advance


Solution

  • I got bitten by this in the past as well. link_to is a rails views helper, using this inside arbres output capturing doesn't work well, unless you enclose it in a span like so:

    span do
      link_to ...
    end
    

    Another way, this is more arbre-like, is to spell out the a tag and say:

    if attachment.object.pdf_file.present?
      span { 'Uploaded PDF:' }
      a(href: attachment.object.pdf_file_url) do
        attachment.object.pdf_file.filename.upcase
      end
    end
    

    Let me know if it works.