5

I'm creating an application that tracks users and achievements (think, xbox live, etc.) These tables are linked via a join table. I would like to have a search form on my index that lets users type in a users name and a new page is loaded with a list of all achievements that user has earned. I'm not entirely sure how to set up this search form, on the index, to actually search the user table and return the results on a new page. Any help would be greatly appreciated. If you require more information then I'll be happy to provide it.

SalsaSalsa
  • 139
  • 2
  • 2
  • 10

4 Answers4

7

Here's a bit of skeleton code to get you started based off what I think you need from what you have said. I hope this is useful.

For the search bit you could 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 view:

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

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

You would, of course, need to ensure the users and achievement models are correctly linked:

class User << ActiveRecord::Base
  has_many :achievements

end
Dave A-R
  • 1,159
  • 1
  • 11
  • 22
2

There are plenty of tutorials and things about this e.g.:

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

Look the thing is every basic explanation in Rails3 starting with the Initial Tutorial provided by them explains you how to setup a new Controller/Model. The example was only one of thousands explaining the same problem.

It is a very broad range of different things you can do to achieve this. Basically you have to put some code in the controller:

  1. which handles the search (including the activerecord stuff or whichever technique you use to access your model)
  2. which sets some variables necessary for the search form

Setup two routes etc... Its to broad and completely covered even by the basic official rails3 tutorial.

fyr
  • 20,227
  • 7
  • 37
  • 53
  • 1
    Thanks, but that tutorial doesn't give a very clear explanation, to me at least, and it requires you use formtastic. Thanks for google search but if it could be broken down a little easier for me to understand, again, I'd appreciate it. – SalsaSalsa Jul 04 '11 at 06:06
  • is there any translation of that link? – Ali Ghanavatian Aug 02 '15 at 05:32
  • @AliQanavatian you can simply click on the right side english as Language – fyr Sep 04 '15 at 19:13
0

Here is an application based on searchlogic is very useful and you can search by whatever you want

   https://github.com/railscasts/176-searchlogic
Charlie Brown
  • 219
  • 3
  • 10
0

You may want to check out the Ransack gem. https://github.com/activerecord-hackery/ransack

dannyk
  • 259
  • 1
  • 6
  • 21