0

Having a bit of an issue:

I am allowing my users to search for people they know by email or username. This is working as expected but I'm having a hard time getting a redirect_to or render to work.

How do I tell rails to do something such as render a template or redirect if:

  1. sphinx returned 0 results"
  2. Nothing has been filled in..

Should my search form instance even be in the index action:

class SearchesController < ApplicationController

   def index

     @users = User.search params[:search]

     @default_photo = Photo.find(@users.first.profile.photo_id) if @users.first.profile.photo_id
   end
end

Look how messy my view is:

<% if params[:search].blank? != true  %>
<h1> Find more people </h1>
<p> Type in the email address, username or full name of who ever you're trying to find
<br />
<br />
<br />
<% else %>

<h1> Find People </h1>
<p> Type in the email address, username or full name of who ever you're trying to find
<br />
<br />
<br />

<% end %>
<%= form_tag searches_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
  <% end %>
<br />
<br />
<br />

<% if params[:search].blank? != true %>
<div id="paintings">
  <% @users.each do |u| %>
<h2> We found: </h2>
    <div class="painting">
      <div class="gallerycontainer">
<% if @default_photo %>
<a class="thumbnail" href="#thumb"><%= link_to image_tag(u.profile.photo(u.profile.photo_id).image, :width => '100'), u.username %>
<% else %>
<a class="thumbnail" href="#thumb"><%= link_to image_tag("/assets/default_avatar.jpg", :width =>"100"), u.username %> <br />
<% end %>


</div>
      <div class="name"><%= link_to u.username, u.username %></div>
      <div class="actions">
       <%= u.profile.first_name %> <%= u.profile.last_name %>
      </div>
    </div>
  <% end %>
  <div class="clear"></div>
</div>

<% end %>

I've had to resort to all of this and I think it's beginning to cause confusion.

If I could do the above steps in a more straight forward way I could move on and complete this task with ease.

For now when user fills in nothing it brings up the same page with just the search field but when they fill in info of a non-existent user I get undefined method profile. If I could deal with this in the controller things would be so much easier and I wouldn't need to resort to this mess in the view.

Kind regards

LondonGuy
  • 10,778
  • 11
  • 79
  • 151

2 Answers2

0

You can redirect to different actions in your controller, like so:

def index
    if(some_condition)
        redirect_to new_model_path
    else
        redirect_to models_path
    end
end

And then you can conditionally redirect.

In views, you can render different partials the same way:

<% if(some_condition) %>
    <%= render :partial => "my_partial" %>
<% else %>
    <%= render :partial => "another_partial" %>
<% end %>

So yeah, see if you can extract some of your view in to partials and either have them on their own page that you can redirect to, or just use conditional partial rendering in your view.

MrDanA
  • 11,489
  • 2
  • 36
  • 47
  • Hi, I understand how to redirect and render templates. The problem is finding a way to tell rails that sphinx returned 0 results so I can tell rails to redirect or render a template in my index action. Also I need to do the same with blank search fields. If they're blank or non-existent user has been typed in redirect or render the same template. – LondonGuy Jan 25 '12 at 20:09
0

Using .empty? or blank? solved my issue. I opened up console and purposely supplied a user that didn't exist and saw the empty array [] so first used == [] in my controller which worked but thought to myself there must be a better way to represent an empty array.

Done some googling and ended up at this post

Ended up using empty? in my controller and all issues are solved.

Community
  • 1
  • 1
LondonGuy
  • 10,778
  • 11
  • 79
  • 151