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.
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.
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?
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.
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.
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.
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.
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.