views:

55

answers:

3

Hello
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?

    +1  A: 

    Try this…

    <%- if @client.address.blank? %>
      "The Address is not set"
    <%- else %>
      <%=h @client.address %>
    <%- end %>
    
    Paul Groves
    +4  A: 
    <%=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.

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

    Lots of ways, here's a somewhat messy one

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