winter sun
winter sun

Reputation: 582

Conditional value in ruby on rails?

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?

    Upvotes: 2

    Views: 1313

    Answers (3)

    EmFi
    EmFi

    Reputation: 23450

    <%=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.

    Upvotes: 5

    Anand Shah
    Anand Shah

    Reputation: 14913

    Lots of ways, here's a somewhat messy one

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

    Upvotes: 2

    Paul Groves
    Paul Groves

    Reputation: 4051

    Try this…

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

    Upvotes: 1

    Related Questions