Display by alphabetical letter.

Hey all, so I have a table of games in my rails application. The table has many of records within in.

What I wish to do is have at the top of the page a sort of link system like the following

A | B | C | D | .................. | X | Y | Z |

What I want to do is when you click on a letter it will then display each of the games by their alphabetical record.

It will do this by searching the game_name column but only extracting those with the first letter not that of which it includes the letter anywhere in the name.

How would I go about doing this. I have done search systems before but can only do one search for a page based on a column. I want it to be a search in which any letter can be clicked and then will link you to associated games.

I am a new user of rails so any support would be of help.

Thanks Christopher Jones

Assuming it is the find itself that is the problem try something like Games.where( "name LIKE ?", "#{params[:letter]}%" )

You can play with this in the rails console to get the syntax right. Obviously in that case you would not use params, but just a letter.

Colin

Colin Law wrote in post #1047699:

It will do this by searching the game_name column but only extracting those with the first letter not that of which it includes the letter anywhere in the name.

How would I go about doing this. I have done search systems before but can only do one search for a page based on a column. I want it to be a search in which any letter can be clicked and then will link you to associated games.

Assuming it is the find itself that is the problem try something like Games.where( "name LIKE ?", "#{params[:letter]}%" )

You can play with this in the rails console to get the syntax right. Obviously in that case you would not use params, but just a letter.

Colin

So I have in my index.html.erb the following

<p>link_to 'A' </p> | <p>link_to 'B' </p> | <p>link_to 'C' </p> | <p>link_to 'D' </p> | <p>link_to 'E' </p> | <p>link_to 'F' </p> | ...... <p>link_to 'Z' </p>

How would I go about writting the direction of each of these to connect to as you said: Game.where( "name LIKE ?", "#{params[:letter]}%" )?

I am assuming that goes in the game.rb file like my other search is but am not 100% sure on how to connect each letter to each part, do I read in the quoted letter to the params? so it looks at the letter and returns it to the search to get all related results?

Sorry if this is silly, just trying to learn.

Thanks Chris Jones

Colin Law wrote in post #1047699:

It will do this by searching the game_name column but only extracting those with the first letter not that of which it includes the letter anywhere in the name.

How would I go about doing this. I have done search systems before but can only do one search for a page based on a column. I want it to be a search in which any letter can be clicked and then will link you to associated games.

Assuming it is the find itself that is the problem try something like Games.where( "name LIKE ?", "#{params[:letter]}%" )

You can play with this in the rails console to get the syntax right. Obviously in that case you would not use params, but just a letter.

Colin

So I have in my index.html.erb the following

<p>link_to 'A' </p> | <p>link_to 'B' </p> | <p>link_to 'C' </p> | <p>link_to 'D' </p> | <p>link_to 'E' </p> | <p>link_to 'F' </p> | ...... <p>link_to 'Z' </p>

How would I go about writting the direction of each of these to connect to as you said: Game.where( "name LIKE ?", "#{params[:letter]}%" )?

I am assuming that goes in the game.rb file like my other search is but am not 100% sure on how to connect each letter to each part, do I read in the quoted letter to the params? so it looks at the letter and returns it to the search to get all related results?

Something like <%= link_to 'A', games_path(:letter => 'A') %>

will request games_controller#index with params[:letter] 'A', then in index you can find just the appropriate records. Of course you would actually do the links in a loop not individually with 'A', 'B' etc.

Sorry if this is silly, just trying to learn.

Not silly, just lack of knowledge. Have you worked through some tutorials? railstutorial.org is good and is free to use online. Work right through it even though you may think the app it is developing is of no interest to you, you will learn the basic techniques to allow you to develop your own app.

Colin

I shall check out those links, lately I have been listening to the casts on railscasts.org and they have proved very useful.

Do you know of any good tutorials of using links to display associated records of a database?

I tried the following but it does not work:

<%= link_to 'f', games_path(:game_name => 'f') %>

It runs but just returns the full table. I even tried putting in a full record in the brackets such as the following:

<%= link_to 'f', games_path(:game_name => 'Fifa 12') %>

hoping that the result would return that record to see if it works but no luck.

Thanks Chris Jones

I shall check out those links, lately I have been listening to the casts on railscasts.org and they have proved very useful.

I think you would be better to work right through railstutorial before going further so that you will get a better grasp of the basics.

Do you know of any good tutorials of using links to display associated records of a database?

I tried the following but it does not work:

<%= link_to 'f', games_path(:game_name => 'f') %>

It runs but just returns the full table. I even tried putting in a full record in the brackets such as the following:

What do you see in development.log when you click the link - you should be able to see the name letter being passed in to the GET request.

Show us that bit of the log and the code you have written for the action to interpret the value passed in params.

Colin

Started GET "/games?game_name=F" for 127.0.0.1 at 2012-02-20 11:53:51 +0000 Creating scope :page. Overwriting existing method Game.page.   Processing by GamesController#index as HTML   Parameters: {"game_name"=>"Fifa 12"}   [1m[36mGame Load (127.0ms)[0m [1mSELECT `games`.* FROM `games` LIMIT 4 OFFSET 0[0m Rendered games/index.html.erb within layouts/application (333.0ms) Completed 200 OK in 425ms (Views: 137.0ms | ActiveRecord: 287.0ms | Sphinx: 0.0ms)

That is what is in my log when I click the F button of the alphabet.

Thanks Chris Jones

Started GET "/games?game_name=F" for 127.0.0.1 at 2012-02-20 11:53:51 +0000 Creating scope :page. Overwriting existing method Game.page. Processing by GamesController#index as HTML Parameters: {"game_name"=>"Fifa 12"} [1m[36mGame Load (127.0ms)[0m [1mSELECT `games`.* FROM `games` LIMIT 4 OFFSET 0[0m Rendered games/index.html.erb within layouts/application (333.0ms) Completed 200 OK in 425ms (Views: 137.0ms | ActiveRecord: 287.0ms | Sphinx: 0.0ms)

That is what is in my log when I click the F button of the alphabet.

And what code have you written in the index action to interpret the letter from the url? If nothing then /please/ work through the tutorial I suggested so that you will have some idea of what rails is all about.

Colin

Ok so I have been playing around with this search for the past few hours and have done the following but still no result.

I have the following in my index.html.erb:

<% for char in 'A'..'Z' %> <%= link_to( "#{char}",                          :update => "content",                          :char => char,                          :url =>{:action => :live_search }) %> <% end %>

And then I have the following in my games_controller.rb:

  def live_search      puts 'live_search'            @game = Game.find(:all,:conditions => ["lower(game_name)like ?","%" + params[:search].downcase+ "%"])            puts @game            if params['search'].to_s.size < 1            render :nothing => true            else                 if @game.size > 0                    render :partial => 'display', :collection => @game                    else                    render :text => "<li>No results found</li>", :layout => false                    end                 end               end

Thanks for all the help so far. Chris Jones

I haven’t read the entire thread (just some parts)

Maybe this is to basic, but it helped me

http://railscasts.com/episodes/111-advanced-search-form

and maybe this to (because as far as I understand you’re trying to get some pagination… or maybe I’m wrong

http://railscasts.com/episodes/240-search-sort-paginate-with-ajax

The approach on the first one will help you with this

@game = Game.find(:all,:conditions => [“lower(game_name) like ?”,“%” + params[:search].downcase+ “%”])

Javier Q.

Javier Quarite wrote in post #1047871:

Have a look at the Rails Guide on Debugging for various debugging techniques to help you work out why the code is not working.

Colin

Colin Law wrote in post #1047882:

But you're searching on a condition for games that contain the character, instead of games that start with the character.

But you're searching on a condition for games that contain the character, instead of games that start with the character.

What would I change to display by starting character?

Also it does not seem to be searching it regardless because if for example I click the character X (a character that none of the records at the moment contains) it still returns them all and if I click the letter F (a character only one of the records contains) it still returns them all.

Any ideas of what my problem might be?

Have a look at the Rails Guide on Debugging for various debugging techniques to help you work out why the code is not working.

Colin

Colin Law wrote in post #1047891:

Any ideas of what my problem might be?

Have a look at the Rails Guide on Debugging for various debugging techniques to help you work out why the code is not working.

Colin

Ok Colin,

From what I have understood it isn't actually connecting with the controller method, simply passing the params in the view text.

This is what I have in my log:

Started GET "/games?char=F&update=content&url%5Baction%5D=live_search" for 127.0.0.1 at 2012-02-20 22:14:02 +0000 Creating scope :page. Overwriting existing method Game.page.   Processing by GamesController#index as HTML   Parameters: {"char"=>"F", "update"=>"content", "url"=>{"action"=>"live_search"}}   [1m[36mGame Load (1.0ms)[0m [1mSELECT `games`.* FROM `games` LIMIT 4 OFFSET 0[0m   [1m[35m (1.0ms)[0m SELECT COUNT(*) FROM `games` Rendered games/index.html.erb within layouts/application (78.0ms) Creating scope :page. Overwriting existing method User.page.   [1m[36mUser Load (1.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = ? LIMIT 1[0m [["id", 9]] Completed 200 OK in 392ms (Views: 360.0ms | ActiveRecord: 31.0ms | Sphinx: 0.0ms)

As you can see it has the parameters and then just moves on but I am not sure why.

Thanks Chris Jones.

Hint: look at the wildcards in the query you are building...