Rails Search Form

I am creating an application that tracks users and achievements (think xbox live, etc.). These tables are linked through a connection table. I would like to have a search form on my index that allows users to enter a username, and a new page loads with a list of all the achievements that the user has earned. I'm not quite sure how to set up this index search form to actually search for a user table and return results on a new page. Any help would be greatly appreciated. If you need more information, I will be happy to provide it.

+5
source share
4 answers

Here's some skeletal code to get you started based on what I think is necessary from what you said. Hope this is helpful.

For the search bit, you can do something like this in your index view:

<%= form_for User.new, :url => "search" do |f| %>
  <%= f.label :name %>
  <%- f.text_field :name %>
<%- end %>

In your controller:

def search
  q = params[:user][:name]
  @users = User.find(:all, :conditions => ["name LIKE %?%",q])
end

and in your search mode:

<%-@users.each do |user| %>
  Name: <%=user.name %>

  <%- user.achievements.each do |achievement| %>
    <%= achievement.name %>
  <%- end %>
<%- end %>

Of course, you need to make sure that users and achievement models are correctly connected:

class User << ActiveRecord::Base
  has_many :achievements

end
+5
source

There are many lessons and things to this:

http://blog.devinterface.com/2010/05/how-to-model-a-custom-search-form-in-rails/

See how each basic explanation in Rails3, starting with the Inception Tutorial they provided, explains to you how to set up a new controller / model. An example was only one in thousands explaining the same problem.

, . :

  • ( activerecord , ).
  • ,

.. rails3.

+2

, searchlogic, , .

   https://github.com/railscasts/176-searchlogic
0

All Articles