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:
- sphinx returned 0 results"
- 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