Topic: [SOLVED] An index view in need of a major refactor

Hello this is my index view of my entries controller which uses a lot conditional logic to show address book data if it exists for a particular entry. I have two questions: is there a simpler way of writing the <%- unless entry.paddress.nil? || entry.paddress.empty? -%> parts and why won't the "Nothing was found. Please try another search term." ever appear? If I search for something that doesn't exist, it just returns nothing in the view instead of the string: "Nothing was found. Please try another search term." I'm fairly sure it has something to do with entries_count in the controller code, but I'm at a loss as to what the problem is. With "@entries, @entries_count = @search.all, @search.count" in the index action of the controller it seems like it should be working correctly. Does anyone see something I do not? Any help would be greatly appreciated.

My index view:

<% @entries.each do |entry| %>
  <%- if @entries_count > 0 -%>
    <div class="entry">
    <h2><%= link_to h(entry.name), entry %></h2>

    <%- unless entry.paddress.nil? || entry.paddress.empty? -%> 
      <h3> <a href="http://maps.google.com?q=<%= h(entry.paddress) %>"><%= h(entry.paddress) %></a></h3>
    <%- end -%>

    <%- unless entry.address.empty? -%>
      <h3>Location:  <a href="http://maps.google.com?q=<%= h(entry.address) %>"><%= h(entry.address) %></a></h3>
    <%- end -%>

    <%- unless entry.category.nil? || entry.category.empty? -%>
      <h3><%= h(entry.category) %></h3>
    <%- end -%>

    <%- unless entry.description.nil? || entry.description.empty? -%>
      <h3><%= h(entry.description) %></h3>
    <%- end -%>

    <p2><%= link_to "more...", entry %><p2>

    <p>
      <%- if current_user -%>
        <%= link_to 'Edit', edit_entry_path(entry) %>
      <%- end -%>
    </p>
  </div>  
  <%- else -%>
    <div class="entry">
      <p>Nothing was found. Please try another search term.</p>
    </div>
  <%- end -%>
<% end %>

<%= will_paginate @entries %>

<%- if current_user -%>
  
<%- else -%>
  <!-- AD CODE-->
<%- end -%>

with it's associated method in the controller:

  def index
    @search = Entry.search_for(params[:search])
    @entries, @entries_count = @search.all, @search.count
    @entries = Entry.search_for(params[:search]).paginate(:page => params[:page], :per_page => 10)
    respond_to do |format|
      format.html
      format.xml  { render :xml => @entries }
    end
  end

Thank you for your time.

Last edited by BasicObject (2010-02-15 03:15:03)

Re: [SOLVED] An index view in need of a major refactor

For your second question (why won't nothing was found...), the reason is that you put the if statement checking the condition within your loop (so if the loop is never executed, because no entries exist, the statement will never get printed). Try moving it outside the loop over each entry.

Also, I wouldn't bother doing the count logic within the controller. It makes the code more difficult to read in my opinion and needlessly complicates the controller. Try:

<%- if @entries.empty? -%>
  <p>Nothing found...</p>
<%- end -%>

<%- @entries.each do |entry| -%>
  <div class="entry">
    ...
  </div>
<%- end -%>

For the first question, I don't think a way exists (what type of association are you using; have you tried dropping the empty check?).

Kevin

Re: [SOLVED] An index view in need of a major refactor

Thanks Kevin, that made things much simpler. For the <%- unless entry.paddress.nil? || entry.paddress.empty? -%> code I was originally just using .empty? but I noticed that after I removed all the characters from a field after there had been a previous value, I would receive not nil errors so I added the .nil? condition. It's okay if there isn't a way to shorten it, I just thought there may have been something that combined .nil? and .empty?. I'm fairly new to ruby. Thanks again.