Question

I'm wondering how I can check for Empty SQL strings in Ruby on rails, since checking if nil do not work if empty and checking if it's = "" do not work either.

          <% if @model.option4 != nil or @poll.option4 != "" %>
          <div class="form-group">
            <div class="col-lg-10">
               <p>POST STUFF ABOUT IT</p>
            </div>
          </div>
      <% end %>

It works perfect to check if it's nil but not when checking if empty. Is there another way to check this? What am I doing wrong?

enter image description here

I'm using DataMapper and SQLite to handle the database. Not ActiveRecord.

      Model.create(question: params["question"],
      option1: params["option1"],
      option2: params["option2"],
      option3: params["option3"],
      option4: params["option4"],
      option5: params["option5"],
      created_on: Time.now,
      user: @user)
Was it helpful?

Solution

Try:

<% unless @model.option4.blank? %>

In your code you most likely misused or when you needed and. However using blank? or present? methods is a common rails way to handle both cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top