Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8519337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:13:15+00:00 2026-06-11T06:13:15+00:00

Trying to come up with a more compact way of expressing this conditional in

  • 0

Trying to come up with a more compact way of expressing this conditional in HAML and Ruby, perhaps with a ternary operator:

- if @page.nil?
  %br (nothing yet)
- else
  %br #{@page.name}

(looking for similar approach as per Neat way to conditionally test whether to add a class in HAML template)

Your help would be appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:13:16+00:00Added an answer on June 11, 2026 at 6:13 am

    The code you have makes the text a child of the <br> element; that is not desirable. What you really meant, I think, was:

    %br
    - if @page.nil?
      (nothing yet)
    - else
      #{@page.name}
    

    For this you can simply do:

    %br
    #{@page.nil? ? "(nothing yet)" : @page.name}
    

    Or

    %br
    = @page.nil? ? "(nothing yet)" : @page.name
    

    Or simply:

    <br>#{@page ? @page.name : "(nothing yet)"}
    

    However, personally I would ‘fix’ this in the controller so that you always have a @page, with something like:

    unless @page
      @page = Page.new( name:"(nothing yet)", … )
    end
    

    With this you can stub out what a new/empty/nothing page looks like and let your view treat it like any other. Your @page still won’t have a @page.id, so you can use that for tests to decide if you are creating a new item or editing an existing one.

    This is how I handle all my forms that may be used to create or edit an item: provide defaults by creating (but not adding to the database) an item with the default values.

    Aside: You’re creating a <br> which is almost never a good idea, and you’re creating it with Haml which should not be used for content markup. You might step back and think about what you’re doing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to come up with a way combine two arrays that have different
I'm trying to come up with an efficient way to overwrite 2 strings that
I am trying to come up with a very fast algorithm for calculating this
I'm trying to come up with a good way to do authentication and authorization.
Background: I have been charged with trying to come up with a new way
Using jQuery and jQuery.Colors, I've been trying to come up with a way to
Continuing my journey into more advanced OO, I'm trying to come up with the
I am trying to come up with a way to raise a public event
I'm trying to come up with a consistent way of representing each factor in
I'm trying to come up with the best way to add the total time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.