0
votes

Ok I was able to overcome the mountain with my searches and getting the results to display properly. However, I have notice that only one of my search options work. The rest gives a "'nil' is not an ActiveModel-compatible object that returns a valid partial path." even when I return the coding back to old. This is strange because my search works perfect for Ethnicity, but for the other parts of the form it returns that error. Perhaps some fresh eyes can catch the culprit.

To clarify on my advanced search page. If I do an Ethnicity search, results pull up. If I do any other search such as for religion, gender, children, etc it gives the nil error. Just doesn't make sense that one option from the search page works and the rest don't when all of them have the same coding. Seems like it's not finding users for it. The error points to " 3 <%= render @users %>"

show.html:

<h1>Search Results</h1>

<%= render @users %>

searches controller:

  def new
    @search = Search.new
  end

  def create
    @search = Search.new(params[:search])
    if @search.save
      redirect_to @search
    else
      render 'new'
    end
  end

  def show
    @search = Search.find(params[:id])
    @users = @search.users
    end

end

search model:

 attr_accessible :age, :children, :ethnicity, :gender, :religion, :zip_code

  def users
    @users ||= find_users
  end

    private

  def find_users
    users = User.order(:id)
    users = users.where(gender: gender) if gender.present?
       users = users.where(zip_code: zip_code) if zip_code.present?
       users = users.where(children: children) if children.present?
       users = users.where(religion: religion) if religion.present?
       users = users.where(ethnicity: ethnicity) if ethnicity.present?
  end
end

search form (ethnicity at bottom shows with no error):

<h1>Advanced Search</h1>

<%= form_for @search do |f| %>
<div class="field">
  <%= f.label :gender %><br />
  <%= f.select :gender, ['man', 'woman'], :include_blank => true %>
</div>

  <div class="field">
    <%= f.label :zip_code %><br />
    <%= f.text_field :zip_code %>
  </div>
  <div class="field">
    <%= f.label :children %><br />
    <%= f.select :children, ['Yes, they live with me', 'I want kids now', "I want one someday", "Not for me"], :include_blank => true %>
  </div>
  <div class="field">
    <%= f.label :religion %><br />
    <%= f.select :religion, [["Agnostic", "1"], ["Atheist", "2"], ["Christian", "3"], ["Catholic", "4"], ["Buddhist", "5"], ["Hindu", "6"], ["Jewish", "7"], ["Muslim", "8"], ["Spiritual without affiliation", "9"], ["Other", "10"], ["None", "11"], ["Prefer not to say", "12"]], :include_blank => true %>
  </div>
  <div class="field">
    <%= f.label :ethnicity %><br />
    <%= f.select :ethnicity, [["Asian", "1"], ["Biracial", "2"], ["Indian", "3"], ["Hispanic/Latin", "4"], ["Middle Eastern", "5"], ["Native American", "6"], ["Pacific Islander", "7"], ["White", "8"], ["Other", "9"]], :include_blank => true %>
  </div>
  <div class="actions"><%= f.submit "Search" %></div>
<% end %>
1

1 Answers

4
votes

That's because at the end of the find_users method you need to return the array:

def find_users
  users = User.order(:id)
  users = users.where(gender: gender) if gender.present?
  users = users.where(zip_code: zip_code) if zip_code.present?
  users = users.where(children: children) if children.present?
  users = users.where(religion: religion) if religion.present?
  users = users.where(ethnicity: ethnicity) if ethnicity.present?
  users # this line is important!
end

It only works with ethnicity because that's the last one, if ethnicity is missing this method will return nil rather than the users array.