Search code examples
ruby-on-railsslim-lang

Rails view - on 'try' failure display something else


I have this variable:

@preview

Which is either a hash filled with wonderful data, or it is a string with some value like preview is unavailable.

I have this code here (slim / rails):

.well
  .preview-data
    - @preview.try(:each) do |line_number, line_data|
        .line#{line_number} = line_data

How do I get the view to display a message if the @preview.try(:each) fails?

What I've tried:

- if @preview.try(:each) 
    @preview.each do |line_number, line_data|
...
- else
    .preview-unavailable = @preview

which just displays the @preview data anyway.


Solution

  • - if @preview.is_a?(String)
        .preview-unavailable = @preview
    - else
        @preview.each do ...