2

I have a form, in which I can set various client fields like client name client address and more.
Only the client name is a must field, and all the other fields can be empty.
After saving the client data I display to the end user the clients details page (with all the clients information).
Now if the address field is empty I want to display some customized text for example "The Address is not set". Currently my "show" page display only this

  • Address <%=h @client.address %>
  • So if the address is empty I see nothing.
    Can any one tell me how I can add this conditional text?

    3 Answers 3

    5
    <%=h @client.address.present? && @client.address || "The Address is not set"%>
    

    If you're using it often enough, you may consider making this a helper.

    Sign up to request clarification or add additional context in comments.

    4 Comments

    For some reason it didn't work for me. The input remains empty
    Sorry, I forgot that blank values are stored as "" and not nil.
    Instead of [email protected]? you can use @client.address.present? which is a little more readable.
    Beerlington: I was not aware of that. Updated with your suggestion.
    2

    Lots of ways, here's a somewhat messy one

    <%=h (@client.address.blank? ? "The Address is not set" : @client.address) %> 
    

    Comments

    1

    Try this…

    <%- if @client.address.blank? %>
      "The Address is not set"
    <%- else %>
      <%=h @client.address %>
    <%- end %>
    

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.